async def test_lights_all_dimmable(hass, aiohttp_client): """Test CONF_LIGHTS_ALL_DIMMABLE.""" # create a lamp without brightness support hass.states.async_set("light.no_brightness", "on", {}) await setup.async_setup_component( hass, http.DOMAIN, {http.DOMAIN: {http.CONF_SERVER_PORT: HTTP_SERVER_PORT}} ) await hass.async_block_till_done() hue_config = { emulated_hue.CONF_LISTEN_PORT: BRIDGE_SERVER_PORT, emulated_hue.CONF_EXPOSE_BY_DEFAULT: True, emulated_hue.CONF_LIGHTS_ALL_DIMMABLE: True, } with patch("homeassistant.components.emulated_hue.create_upnp_datagram_endpoint"): await setup.async_setup_component( hass, emulated_hue.DOMAIN, {emulated_hue.DOMAIN: hue_config}, ) await hass.async_block_till_done() config = Config(None, hue_config) config.numbers = ENTITY_IDS_BY_NUMBER web_app = hass.http.app HueOneLightStateView(config).register(web_app, web_app.router) client = await aiohttp_client(web_app) light_without_brightness_json = await perform_get_light_state( client, "light.no_brightness", HTTP_OK ) assert light_without_brightness_json["state"][HUE_API_STATE_ON] is True assert light_without_brightness_json["type"] == "Dimmable light" assert ( light_without_brightness_json["state"][HUE_API_STATE_BRI] == HUE_API_STATE_BRI_MAX )
def test_config_google_home_entity_id_to_number(): """Test config adheres to the type.""" conf = Config(Mock(), { 'type': 'google_home' }) mop = mock_open(read_data=json.dumps({'1': 'light.test2'})) handle = mop() with patch('homeassistant.components.emulated_hue.open', mop, create=True): number = conf.entity_id_to_number('light.test') assert number == '2' assert handle.write.call_count == 1 assert json.loads(handle.write.mock_calls[0][1][0]) == { '1': 'light.test2', '2': 'light.test', } number = conf.entity_id_to_number('light.test') assert number == '2' assert handle.write.call_count == 1 number = conf.entity_id_to_number('light.test2') assert number == '1' assert handle.write.call_count == 1 entity_id = conf.number_to_entity_id('1') assert entity_id == 'light.test2'
def test_config_google_home_entity_id_to_number_empty(): """Test config adheres to the type.""" mock_hass = Mock() mock_hass.config.path = MagicMock("path", return_value="test_path") conf = Config(mock_hass, { 'type': 'google_home' }) with patch('homeassistant.components.emulated_hue.load_json', return_value={}) as json_loader: with patch('homeassistant.components.emulated_hue' '.save_json') as json_saver: number = conf.entity_id_to_number('light.test') assert number == '1' assert json_saver.call_count == 1 assert json_loader.call_count == 1 assert json_saver.mock_calls[0][1][1] == { '1': 'light.test', } number = conf.entity_id_to_number('light.test') assert number == '1' assert json_saver.call_count == 1 number = conf.entity_id_to_number('light.test2') assert number == '2' assert json_saver.call_count == 2 entity_id = conf.number_to_entity_id('2') assert entity_id == 'light.test2'
def test_config_google_home_entity_id_to_number(): """Test config adheres to the type.""" mock_hass = Mock() mock_hass.config.path = MagicMock("path", return_value="test_path") conf = Config(mock_hass, { 'type': 'google_home' }) mop = mock_open(read_data=json.dumps({'1': 'light.test2'})) handle = mop() with patch('homeassistant.util.json.open', mop, create=True): with patch('homeassistant.util.json.os.open', return_value=0): with patch('homeassistant.util.json.os.replace'): number = conf.entity_id_to_number('light.test') assert number == '2' assert handle.write.call_count == 1 assert json.loads(handle.write.mock_calls[0][1][0]) == { '1': 'light.test2', '2': 'light.test', } number = conf.entity_id_to_number('light.test') assert number == '2' assert handle.write.call_count == 1 number = conf.entity_id_to_number('light.test2') assert number == '1' assert handle.write.call_count == 1 entity_id = conf.number_to_entity_id('1') assert entity_id == 'light.test2'
def hue_client(loop, hass_hue, aiohttp_client): """Create web client for emulated hue api.""" web_app = hass_hue.http.app config = Config( None, { emulated_hue.CONF_ENTITIES: { "light.bed_light": { emulated_hue.CONF_ENTITY_HIDDEN: True }, # Kitchen light is explicitly excluded from being exposed "light.kitchen_lights": { emulated_hue.CONF_ENTITY_HIDDEN: True }, # Ceiling Fan is explicitly excluded from being exposed "fan.ceiling_fan": { emulated_hue.CONF_ENTITY_HIDDEN: True }, # Expose the script "script.set_kitchen_light": { emulated_hue.CONF_ENTITY_HIDDEN: False }, # Expose cover "cover.living_room_window": { emulated_hue.CONF_ENTITY_HIDDEN: False }, # Expose Hvac "climate.hvac": { emulated_hue.CONF_ENTITY_HIDDEN: False }, # Expose HeatPump "climate.heatpump": { emulated_hue.CONF_ENTITY_HIDDEN: False }, # No expose setting (use default of not exposed) "climate.nosetting": {}, }, }, ) config.numbers = ENTITY_IDS_BY_NUMBER HueUsernameView().register(web_app, web_app.router) HueAllLightsStateView(config).register(web_app, web_app.router) HueOneLightStateView(config).register(web_app, web_app.router) HueOneLightChangeView(config).register(web_app, web_app.router) HueAllGroupsStateView(config).register(web_app, web_app.router) HueFullStateView(config).register(web_app, web_app.router) HueConfigView(config).register(web_app, web_app.router) return loop.run_until_complete(aiohttp_client(web_app))
def test_config_alexa_entity_id_to_number(): """Test config adheres to the type.""" conf = Config({"type": "alexa"}) number = conf.entity_id_to_number("light.test") assert number == "light.test" number = conf.entity_id_to_number("light.test") assert number == "light.test" number = conf.entity_id_to_number("light.test2") assert number == "light.test2" entity_id = conf.number_to_entity_id("light.test") assert entity_id == "light.test"
def test_config_google_home_entity_id_to_number(): """Test config adheres to the type.""" conf = Config({"type": "google_home"}) number = conf.entity_id_to_number("light.test") assert number == "1" number = conf.entity_id_to_number("light.test") assert number == "1" number = conf.entity_id_to_number("light.test2") assert number == "2" entity_id = conf.number_to_entity_id("1") assert entity_id == "light.test"
def hue_client(loop, hass_hue, aiohttp_client): """Create web client for emulated hue api.""" web_app = hass_hue.http.app config = Config( None, { emulated_hue.CONF_TYPE: emulated_hue.TYPE_ALEXA, emulated_hue.CONF_ENTITIES: { "light.bed_light": { emulated_hue.CONF_ENTITY_HIDDEN: True }, "cover.living_room_window": { emulated_hue.CONF_ENTITY_HIDDEN: False }, }, }, ) HueUsernameView().register(web_app, web_app.router) HueAllLightsStateView(config).register(web_app, web_app.router) HueOneLightStateView(config).register(web_app, web_app.router) HueOneLightChangeView(config).register(web_app, web_app.router) HueAllGroupsStateView(config).register(web_app, web_app.router) HueFullStateView(config).register(web_app, web_app.router) return loop.run_until_complete(aiohttp_client(web_app))
def test_config_google_home_entity_id_to_number(): """Test config adheres to the type.""" conf = Config({ 'type': 'google_home' }) number = conf.entity_id_to_number('light.test') assert number == '1' number = conf.entity_id_to_number('light.test') assert number == '1' number = conf.entity_id_to_number('light.test2') assert number == '2' entity_id = conf.number_to_entity_id('1') assert entity_id == 'light.test'
def test_config_alexa_entity_id_to_number(): """Test config adheres to the type.""" conf = Config(None, { 'type': 'alexa' }) number = conf.entity_id_to_number('light.test') assert number == 'light.test' number = conf.entity_id_to_number('light.test') assert number == 'light.test' number = conf.entity_id_to_number('light.test2') assert number == 'light.test2' entity_id = conf.number_to_entity_id('light.test') assert entity_id == 'light.test'
def hue_client(loop, hass_hue, test_client): """Create web client for emulated hue api.""" web_app = hass_hue.http.app config = Config(None, {'type': 'alexa'}) HueUsernameView().register(web_app.router) HueAllLightsStateView(config).register(web_app.router) HueOneLightStateView(config).register(web_app.router) HueOneLightChangeView(config).register(web_app.router) return loop.run_until_complete(test_client(web_app))
def test_warning_config_google_home_listen_port(): """Test we warn when non-default port is used for Google Home.""" with patch.object(_LOGGER, 'warning') as mock_warn: Config({ 'type': 'google_home', 'host_ip': '123.123.123.123', 'listen_port': 8300 }) assert mock_warn.called assert mock_warn.mock_calls[0][1][0] == \ "When targetting Google Home, listening port has to be port 80"
def test_config_google_home_entity_id_to_number_empty(): """Test config adheres to the type.""" mock_hass = Mock() mock_hass.config.path = MagicMock("path", return_value="test_path") conf = Config(mock_hass, {"type": "google_home"}) with patch( "homeassistant.components.emulated_hue.load_json", return_value={} ) as json_loader, patch( "homeassistant.components.emulated_hue.save_json" ) as json_saver: number = conf.entity_id_to_number("light.test") assert number == "1" assert json_saver.call_count == 1 assert json_loader.call_count == 1 assert json_saver.mock_calls[0][1][1] == {"1": "light.test"} number = conf.entity_id_to_number("light.test") assert number == "1" assert json_saver.call_count == 1 number = conf.entity_id_to_number("light.test2") assert number == "2" assert json_saver.call_count == 2 entity_id = conf.number_to_entity_id("2") assert entity_id == "light.test2"
async def test_config_google_home_entity_id_to_number_altered(hass, hass_storage): """Test config adheres to the type.""" conf = Config(hass, {"type": "google_home"}) hass_storage[DATA_KEY] = { "version": DATA_VERSION, "key": DATA_KEY, "data": {"21": "light.test2"}, } await conf.async_setup() number = conf.entity_id_to_number("light.test") assert number == "22" async_fire_time_changed(hass, utcnow() + timedelta(seconds=SAVE_DELAY)) await hass.async_block_till_done() assert hass_storage[DATA_KEY]["data"] == { "21": "light.test2", "22": "light.test", } number = conf.entity_id_to_number("light.test") assert number == "22" number = conf.entity_id_to_number("light.test2") assert number == "21" entity_id = conf.number_to_entity_id("21") assert entity_id == "light.test2"
def test_config_google_home_entity_id_to_number(): """Test config adheres to the type.""" conf = Config(Mock(), {'type': 'google_home'}) mop = mock_open(read_data=json.dumps({'1': 'light.test2'})) handle = mop() with patch('homeassistant.components.emulated_hue.open', mop, create=True): number = conf.entity_id_to_number('light.test') assert number == '2' assert handle.write.call_count == 1 assert json.loads(handle.write.mock_calls[0][1][0]) == { '1': 'light.test2', '2': 'light.test', } number = conf.entity_id_to_number('light.test') assert number == '2' assert handle.write.call_count == 1 number = conf.entity_id_to_number('light.test2') assert number == '1' assert handle.write.call_count == 1 entity_id = conf.number_to_entity_id('1') assert entity_id == 'light.test2'
def test_config_google_home_entity_id_to_number_empty(): """Test config adheres to the type.""" mock_hass = Mock() mock_hass.config.path = MagicMock("path", return_value="test_path") conf = Config(mock_hass, {'type': 'google_home'}) with patch('homeassistant.components.emulated_hue.load_json', return_value={}) as json_loader: with patch('homeassistant.components.emulated_hue' '.save_json') as json_saver: number = conf.entity_id_to_number('light.test') assert number == '1' assert json_saver.call_count == 1 assert json_loader.call_count == 1 assert json_saver.mock_calls[0][1][1] == { '1': 'light.test', } number = conf.entity_id_to_number('light.test') assert number == '1' assert json_saver.call_count == 1 number = conf.entity_id_to_number('light.test2') assert number == '2' assert json_saver.call_count == 2 entity_id = conf.number_to_entity_id('2') assert entity_id == 'light.test2'
def hue_client(loop, hass_hue, test_client): """Create web client for emulated hue api.""" web_app = hass_hue.http.app config = Config(None, { emulated_hue.CONF_TYPE: emulated_hue.TYPE_ALEXA, emulated_hue.CONF_ENTITIES: { 'light.bed_light': { emulated_hue.CONF_ENTITY_HIDDEN: True } } }) HueUsernameView().register(web_app.router) HueAllLightsStateView(config).register(web_app.router) HueOneLightStateView(config).register(web_app.router) HueOneLightChangeView(config).register(web_app.router) return loop.run_until_complete(test_client(web_app))
def test_config_alexa_entity_id_to_number(): """Test config adheres to the type.""" conf = Config(None, {"type": "alexa"}) number = conf.entity_id_to_number("light.test") assert number == "light.test" number = conf.entity_id_to_number("light.test") assert number == "light.test" number = conf.entity_id_to_number("light.test2") assert number == "light.test2" entity_id = conf.number_to_entity_id("light.test") assert entity_id == "light.test"
def test_config_alexa_entity_id_to_number(): """Test config adheres to the type.""" conf = Config(None, {'type': 'alexa'}) number = conf.entity_id_to_number('light.test') assert number == 'light.test' number = conf.entity_id_to_number('light.test') assert number == 'light.test' number = conf.entity_id_to_number('light.test2') assert number == 'light.test2' entity_id = conf.number_to_entity_id('light.test') assert entity_id == 'light.test'
def test_config_google_home_entity_id_to_number(): """Test config adheres to the type.""" conf = Config({'type': 'google_home'}) number = conf.entity_id_to_number('light.test') assert number == '1' number = conf.entity_id_to_number('light.test') assert number == '1' number = conf.entity_id_to_number('light.test2') assert number == '2' entity_id = conf.number_to_entity_id('1') assert entity_id == 'light.test'