def test_api_versions(self): for api_version in [1, 5, 6]: with requests_mock.Mocker() as m: api = m.get("http://{}:1925/{}/system".format( HOST, api_version), text="{}") PhilipsTV(HOST, api_version) self.assertEquals(api.call_count, 1)
def setup_platform(hass, config, add_entities, discovery_info=None): """Set up the Philips TV platform.""" name = config.get(CONF_NAME) host = config.get(CONF_HOST) api_version = config.get(CONF_API_VERSION) turn_on_action = config.get(CONF_ON_ACTION) tvapi = PhilipsTV(host, api_version) on_script = Script(hass, turn_on_action) if turn_on_action else None add_entities([PhilipsTVMediaPlayer(tvapi, name, on_script)])
async def validate_input(hass: core.HomeAssistant, host: str, api_version: int) -> tuple[dict, PhilipsTV]: """Validate the user input allows us to connect.""" hub = PhilipsTV(host, api_version) await hub.getSystem() await hub.setTransport(hub.secured_transport) if not hub.system: raise ConnectionFailure("System data is empty") return hub
def test_v6_empty_channels(self): V = 6 with requests_mock.Mocker() as m: get_response(m.get, V, "system", "1/system") for url in [ "system", "audio/volume", "channeldb/tv", "activities/tv" ]: get_response(m.get, V, url) tv = PhilipsTV(HOST) tv.update() self.assertIsNone(tv.channels) self.assertEquals(tv.sources, []) self.assertIsNotNone(tv.volume)
def test_v1(self): V = 1 with requests_mock.Mocker() as m: for url in [ "system", "audio/volume", "channels", "channels/current" ]: get_response(m.get, V, url) _, sources = get_response(m.get, V, "sources") _, source_id = get_response(m.get, V, "sources/current") tv = PhilipsTV(HOST) tv.update() self.assertIsNotNone(tv.channels) self.assertIsNotNone(tv.sources) self.assertEqual(tv.getSourceName(tv.source_id), sources[source_id["id"]]["name"]) self.assertIsNotNone(tv.volume)
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Set up Philips TV from a config entry.""" system: SystemType | None = entry.data.get(CONF_SYSTEM) tvapi = PhilipsTV( entry.data[CONF_HOST], entry.data[CONF_API_VERSION], username=entry.data.get(CONF_USERNAME), password=entry.data.get(CONF_PASSWORD), system=system, ) coordinator = PhilipsTVDataUpdateCoordinator(hass, tvapi, entry.options) await coordinator.async_refresh() if (actual_system := tvapi.system) and actual_system != system: data = {**entry.data, CONF_SYSTEM: actual_system} hass.config_entries.async_update_entry(entry, data=data)
def _test_api_version_detection(self, api_version): with requests_mock.Mocker() as m: if api_version > 1: detection, _ = get_response(m.get, api_version, "system", "1/system") api, response = get_response(m.get, api_version, "system") tv = PhilipsTV(HOST) tv.getSystem() tv.getName() if api_version > 1: self.assertEquals(detection.call_count, 1) self.assertEquals(api.call_count, 1) else: self.assertEquals(api.call_count, 2) self.assertEquals(tv.api_version, api_version) self.assertEquals(tv.name, response["name"])
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): """Set up Philips TV from a config entry.""" tvapi = PhilipsTV( entry.data[CONF_HOST], entry.data[CONF_API_VERSION], username=entry.data.get(CONF_USERNAME), password=entry.data.get(CONF_PASSWORD), ) coordinator = PhilipsTVDataUpdateCoordinator(hass, tvapi) await coordinator.async_refresh() hass.data.setdefault(DOMAIN, {}) hass.data[DOMAIN][entry.entry_id] = coordinator hass.config_entries.async_setup_platforms(entry, PLATFORMS) return True
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): """Set up Philips TV from a config entry.""" tvapi = PhilipsTV( entry.data[CONF_HOST], entry.data[CONF_API_VERSION], username=entry.data.get(CONF_USERNAME), password=entry.data.get(CONF_PASSWORD), ) coordinator = PhilipsTVDataUpdateCoordinator(hass, tvapi) await coordinator.async_refresh() hass.data[DOMAIN][entry.entry_id] = coordinator for component in PLATFORMS: hass.async_create_task( hass.config_entries.async_forward_entry_setup(entry, component)) return True