async def test_initial_state_overrules_restore_state(hass): """Ensure states are restored on startup.""" mock_restore_cache( hass, (State("input_number.b1", "70"), State("input_number.b2", "200")) ) hass.state = CoreState.starting await async_setup_component( hass, DOMAIN, { DOMAIN: { "b1": {"initial": 50, "min": 0, "max": 100}, "b2": {"initial": 60, "min": 0, "max": 100}, } }, ) state = hass.states.get("input_number.b1") assert state assert float(state.state) == 50 state = hass.states.get("input_number.b2") assert state assert float(state.state) == 60
async def test_off_delay_restore(hass, rfxtrx): """Make sure binary sensor restore as off, if off delay is active.""" mock_restore_cache( hass, [ State( "binary_sensor.ac_118cdea_2", "on", attributes={ATTR_EVENT: EVENT_AC_118CDEA_2_ON}, ) ], ) entry_data = create_rfx_test_cfg( devices={EVENT_AC_118CDEA_2_ON: { "off_delay": 5 }}) mock_entry = MockConfigEntry(domain="rfxtrx", unique_id=DOMAIN, data=entry_data) mock_entry.add_to_hass(hass) await hass.config_entries.async_setup(mock_entry.entry_id) await hass.async_block_till_done() await hass.async_start() state = hass.states.get("binary_sensor.ac_118cdea_2") assert state assert state.state == "off"
async def test_initial_state_overrules_restore_state(opp): """Ensure states are restored on startup.""" mock_restore_cache( opp, (State("counter.test1", "11"), State("counter.test2", "-22")) ) opp.state = CoreState.starting await async_setup_component( opp, DOMAIN, { DOMAIN: { "test1": {CONF_RESTORE: False}, "test2": {CONF_INITIAL: 10, CONF_RESTORE: False}, } }, ) state = opp.states.get("counter.test1") assert state assert int(state.state) == 0 state = opp.states.get("counter.test2") assert state assert int(state.state) == 10
async def test_initial_value_on_but_restore_off(hass): """Test initial value on and restored state is turned off.""" calls = async_mock_service(hass, "test", "automation") mock_restore_cache(hass, (State("automation.hello", STATE_OFF), )) assert await async_setup_component( hass, automation.DOMAIN, { automation.DOMAIN: { "alias": "hello", "initial_state": "on", "trigger": { "platform": "event", "event_type": "test_event" }, "action": { "service": "test.automation", "entity_id": "hello.world" }, } }, ) assert automation.is_on(hass, "automation.hello") hass.bus.async_fire("test_event") await hass.async_block_till_done() assert len(calls) == 1
async def test_restore_state_failed(hass: HomeAssistant) -> None: """Test integration sensor state is restored correctly.""" mock_restore_cache( hass, (State( "sensor.integration", "INVALID", { "last_reset": "2019-10-06T21:00:00.000000", }, ), ), ) config = { "sensor": { "platform": "integration", "name": "integration", "source": "sensor.power", "unit": ENERGY_KILO_WATT_HOUR, } } assert await async_setup_component(hass, "sensor", config) await hass.async_block_till_done() state = hass.states.get("sensor.integration") assert state assert state.state == "0" assert state.attributes.get("unit_of_measurement") == ENERGY_KILO_WATT_HOUR assert state.attributes.get("state_class") == STATE_CLASS_MEASUREMENT assert state.attributes.get("last_reset") != "2019-10-06T21:00:00" assert "device_class" not in state.attributes
def test_no_initial_value_and_restore_off(hass): """Test initial value off and restored state is turned on.""" calls = async_mock_service(hass, 'test', 'automation') mock_restore_cache(hass, (State('automation.hello', STATE_OFF), )) res = yield from async_setup_component( hass, automation.DOMAIN, { automation.DOMAIN: { 'alias': 'hello', 'trigger': { 'platform': 'event', 'event_type': 'test_event', }, 'action': { 'service': 'test.automation', 'entity_id': 'hello.world' } } }) assert res assert not automation.is_on(hass, 'automation.hello') hass.bus.async_fire('test_event') yield from hass.async_block_till_done() assert len(calls) == 0
def test_restore_state(hass): """Ensure states are restored on startup.""" mock_restore_cache(hass, ( State('input_slider.b1', '70'), State('input_slider.b2', '200'), )) hass.state = CoreState.starting yield from async_setup_component( hass, DOMAIN, { DOMAIN: { 'b1': { 'min': 0, 'max': 100, }, 'b2': { 'min': 10, 'max': 100, }, } }) state = hass.states.get('input_slider.b1') assert state assert float(state.state) == 70 state = hass.states.get('input_slider.b2') assert state assert float(state.state) == 10
def test_initial_state_overrules_restore_state(hass): """Ensure states are restored on startup.""" mock_restore_cache(hass, ( State('input_text.b1', 'testing'), State('input_text.b2', 'testing too long'), )) hass.state = CoreState.starting yield from async_setup_component( hass, DOMAIN, { DOMAIN: { 'b1': { 'initial': 'test', 'min': 0, 'max': 10, }, 'b2': { 'initial': 'test', 'min': 0, 'max': 10, }, } }) state = hass.states.get('input_text.b1') assert state assert str(state.state) == 'test' state = hass.states.get('input_text.b2') assert state assert str(state.state) == 'test'
async def test_restore_state(hass: HomeAssistant, enable_custom_integrations: None) -> None: """Test we restore skipped version state.""" mock_restore_cache( hass, ( State( "update.update_available", STATE_ON, # Incorrect, but helps checking if it is ignored { ATTR_SKIPPED_VERSION: "1.0.1", }, ), ), ) platform = getattr(hass.components, f"test.{DOMAIN}") platform.init() assert await async_setup_component(hass, DOMAIN, {DOMAIN: { CONF_PLATFORM: "test" }}) await hass.async_block_till_done() state = hass.states.get("update.update_available") assert state assert state.state == STATE_OFF assert state.attributes[ATTR_INSTALLED_VERSION] == "1.0.0" assert state.attributes[ATTR_LATEST_VERSION] == "1.0.1" assert state.attributes[ATTR_SKIPPED_VERSION] == "1.0.1"
async def test_restore_home_state(hass, hass_admin_user): """Test that the state is restored for a person on startup.""" user_id = hass_admin_user.id attrs = { ATTR_ID: '1234', ATTR_LATITUDE: 10.12346, ATTR_LONGITUDE: 11.12346, ATTR_SOURCE: DEVICE_TRACKER, ATTR_USER_ID: user_id } state = State('person.tracked_person', 'home', attrs) mock_restore_cache(hass, (state, )) hass.state = CoreState.not_running mock_component(hass, 'recorder') config = { DOMAIN: { 'id': '1234', 'name': 'tracked person', 'user_id': user_id, 'device_trackers': DEVICE_TRACKER } } assert await async_setup_component(hass, DOMAIN, config) state = hass.states.get('person.tracked_person') assert state.state == 'home' assert state.attributes.get(ATTR_ID) == '1234' assert state.attributes.get(ATTR_LATITUDE) == 10.12346 assert state.attributes.get(ATTR_LONGITUDE) == 11.12346 # When restoring state the entity_id of the person will be used as source. assert state.attributes.get(ATTR_SOURCE) == 'person.tracked_person' assert state.attributes.get(ATTR_USER_ID) == user_id
def test_initial_state_overrules_restore_state(hass): """Ensure states are restored on startup.""" mock_restore_cache(hass, ( State('input_select.s1', 'last option'), State('input_select.s2', 'bad option'), )) options = { 'options': [ 'first option', 'middle option', 'last option', ], 'initial': 'middle option', } yield from async_setup_component( hass, DOMAIN, {DOMAIN: { 's1': options, 's2': options, }}) state = hass.states.get('input_select.s1') assert state assert state.state == 'middle option' state = hass.states.get('input_select.s2') assert state assert state.state == 'middle option'
async def test_initial_state_overrules_restore_state(hass): """Ensure states are restored on startup.""" mock_restore_cache( hass, ( State("input_select.s1", "last option"), State("input_select.s2", "bad option"), ), ) options = { "options": ["first option", "middle option", "last option"], "initial": "middle option", } await async_setup_component(hass, DOMAIN, {DOMAIN: { "s1": options, "s2": options }}) state = hass.states.get("input_select.s1") assert state assert state.state == "middle option" state = hass.states.get("input_select.s2") assert state assert state.state == "middle option"
async def test_sending_mqtt_commands(hass, mqtt_mock_entry_with_yaml_config): """Test the sending MQTT commands.""" fake_state = ha.State("scene.test", STATE_UNKNOWN) mock_restore_cache(hass, (fake_state, )) assert await async_setup_component( hass, scene.DOMAIN, { scene.DOMAIN: { "platform": "mqtt", "name": "test", "command_topic": "command-topic", "payload_on": "beer on", }, }, ) await hass.async_block_till_done() mqtt_mock = await mqtt_mock_entry_with_yaml_config() state = hass.states.get("scene.test") assert state.state == STATE_UNKNOWN data = {ATTR_ENTITY_ID: "scene.test"} await hass.services.async_call(scene.DOMAIN, SERVICE_TURN_ON, data, blocking=True) mqtt_mock.async_publish.assert_called_once_with("command-topic", "beer on", 0, False)
async def test_restore_state(hass): """Ensure states are restored on startup.""" mock_restore_cache(hass, (State( 'climate.test_thermostat', '0', { ATTR_TEMPERATURE: "20", climate.ATTR_OPERATION_MODE: "off", ATTR_AWAY_MODE: "on" }), )) hass.state = CoreState.starting await async_setup_component( hass, climate.DOMAIN, { 'climate': { 'platform': 'generic_thermostat', 'name': 'test_thermostat', 'heater': ENT_SWITCH, 'target_sensor': ENT_SENSOR, } }) state = hass.states.get('climate.test_thermostat') assert (state.attributes[ATTR_TEMPERATURE] == 20) assert (state.attributes[climate.ATTR_OPERATION_MODE] == "off") assert (state.state == STATE_OFF)
async def test_restore_state_failed(hass: HomeAssistant) -> None: """Test integration sensor state is restored correctly.""" mock_restore_cache( hass, (State( "sensor.integration", "INVALID", {}, ), ), ) config = { "sensor": { "platform": "integration", "name": "integration", "source": "sensor.power", } } assert await async_setup_component(hass, "sensor", config) await hass.async_block_till_done() state = hass.states.get("sensor.integration") assert state assert state.state == "unavailable" assert state.attributes.get("unit_of_measurement") is None assert state.attributes.get("state_class") == STATE_CLASS_TOTAL_INCREASING assert "device_class" not in state.attributes
async def test_restore_state(hass, expected_state): """Ensure state is restored on startup.""" mock_restore_cache(hass, (State("alarm_control_panel.test", expected_state), )) hass.state = CoreState.starting mock_component(hass, "recorder") assert await async_setup_component( hass, alarm_control_panel.DOMAIN, { "alarm_control_panel": { "platform": "manual", "name": "test", "arming_time": 0, "trigger_time": 0, "disarm_after_trigger": False, } }, ) await hass.async_block_till_done() state = hass.states.get("alarm_control_panel.test") assert state assert state.state == expected_state
async def test_restore_home_state(hass, hass_admin_user): """Test that the state is restored for a person on startup.""" user_id = hass_admin_user.id attrs = { ATTR_ID: "1234", ATTR_LATITUDE: 10.12346, ATTR_LONGITUDE: 11.12346, ATTR_SOURCE: DEVICE_TRACKER, ATTR_USER_ID: user_id, } state = State("person.tracked_person", "home", attrs) mock_restore_cache(hass, (state, )) hass.state = CoreState.not_running mock_component(hass, "recorder") config = { DOMAIN: { "id": "1234", "name": "tracked person", "user_id": user_id, "device_trackers": DEVICE_TRACKER, } } with assert_setup_component(1): assert await async_setup_component(hass, DOMAIN, config) state = hass.states.get("person.tracked_person") assert state.state == "home" assert state.attributes.get(ATTR_ID) == "1234" assert state.attributes.get(ATTR_LATITUDE) == 10.12346 assert state.attributes.get(ATTR_LONGITUDE) == 11.12346 # When restoring state the entity_id of the person will be used as source. assert state.attributes.get(ATTR_SOURCE) == "person.tracked_person" assert state.attributes.get(ATTR_USER_ID) == user_id
async def test_restore_state(hass): """Ensure states are restored on startup.""" mock_restore_cache( hass, ( State("input_boolean.b1", "on"), State("input_boolean.b2", "off"), State("input_boolean.b3", "on"), ), ) hass.state = CoreState.starting mock_component(hass, "recorder") await async_setup_component(hass, DOMAIN, {DOMAIN: { "b1": None, "b2": None }}) state = hass.states.get("input_boolean.b1") assert state assert state.state == "on" state = hass.states.get("input_boolean.b2") assert state assert state.state == "off"
def test_async_added_to_hass(hass): """Test resoring state.""" attr = { device_tracker.ATTR_LONGITUDE: 18, device_tracker.ATTR_LATITUDE: -33, device_tracker.ATTR_LATITUDE: -33, device_tracker.ATTR_SOURCE_TYPE: 'gps', device_tracker.ATTR_GPS_ACCURACY: 2, device_tracker.ATTR_BATTERY: 100 } mock_restore_cache(hass, [State('device_tracker.jk', 'home', attr)]) path = hass.config.path(device_tracker.YAML_DEVICES) files = { path: 'jk:\n name: JK Phone\n track: True', } with patch_yaml_files(files): yield from device_tracker.async_setup(hass, {}) state = hass.states.get('device_tracker.jk') assert state assert state.state == 'home' for key, val in attr.items(): atr = state.attributes.get(key) assert atr == val, "{}={} expected: {}".format(key, atr, val)
async def test_initial_state_overrules_restore_state(hass): """Ensure states are restored on startup.""" mock_restore_cache( hass, (State("input_boolean.b1", "on"), State("input_boolean.b2", "off"))) hass.state = CoreState.starting await async_setup_component( hass, DOMAIN, {DOMAIN: { "b1": { CONF_INITIAL: False }, "b2": { CONF_INITIAL: True } }}, ) state = hass.states.get("input_boolean.b1") assert state assert state.state == "off" state = hass.states.get("input_boolean.b2") assert state assert state.state == "on"
def test_initial_state_overrules_restore_state(hass): """Ensure states are restored on startup.""" mock_restore_cache(hass, ( State('input_boolean.b1', 'on'), State('input_boolean.b2', 'off'), )) hass.state = CoreState.starting yield from async_setup_component( hass, DOMAIN, {DOMAIN: { 'b1': { CONF_INITIAL: False }, 'b2': { CONF_INITIAL: True }, }}) state = hass.states.get('input_boolean.b1') assert state assert state.state == 'off' state = hass.states.get('input_boolean.b2') assert state assert state.state == 'on'
async def test_restore_state(hass): """Test utility sensor restore state.""" config = { "utility_meter": { "energy_bill": { "source": "sensor.energy", "tariffs": ["onpeak", "midpeak", "offpeak"], } } } mock_restore_cache( hass, [ State( "select.energy_bill", "midpeak", ), ], ) assert await async_setup_component(hass, DOMAIN, config) assert await async_setup_component(hass, Platform.SENSOR, config) await hass.async_block_till_done() # restore from cache state = hass.states.get("select.energy_bill") assert state.state == "midpeak"
async def test_no_restore_state(hass): """Ensure states are restored on startup if they exist. Allows for graceful reboot. """ mock_restore_cache(hass, (State('climate.test_thermostat', HVAC_MODE_OFF, { ATTR_TEMPERATURE: "20", ATTR_PRESET_MODE: PRESET_AWAY }), )) hass.state = CoreState.starting await async_setup_component( hass, DOMAIN, { 'climate': { 'platform': 'generic_thermostat', 'name': 'test_thermostat', 'heater': ENT_SWITCH, 'target_sensor': ENT_SENSOR, 'target_temp': 22 } }) state = hass.states.get('climate.test_thermostat') assert (state.attributes[ATTR_TEMPERATURE] == 22) assert (state.state == HVAC_MODE_OFF)
def test_no_restore_state(hass): """Ensure states are restored on startup if they exist. Allows for graceful reboot. """ mock_restore_cache(hass, (State( 'climate.test_thermostat', '0', { ATTR_TEMPERATURE: "20", climate.ATTR_OPERATION_MODE: "off", ATTR_AWAY_MODE: "on" }), )) hass.state = CoreState.starting yield from async_setup_component( hass, climate.DOMAIN, { 'climate': { 'platform': 'generic_thermostat', 'name': 'test_thermostat', 'heater': ENT_SWITCH, 'target_sensor': ENT_SENSOR, 'target_temp': 22 } }) state = hass.states.get('climate.test_thermostat') assert (state.attributes[ATTR_TEMPERATURE] == 22) assert (state.state == STATE_OFF)
async def test_restore_state_cover(opp, state): """Run test for cover restore state.""" entity_id = "cover.test" cover_name = "test" config = { CONF_NAME: cover_name, CALL_TYPE_COIL: 1234, CONF_STATE_OPEN: 1, CONF_STATE_CLOSED: 0, CONF_STATE_OPENING: 2, CONF_STATE_CLOSING: 3, CONF_STATUS_REGISTER: 1234, CONF_STATUS_REGISTER_TYPE: CALL_TYPE_REGISTER_HOLDING, } mock_restore_cache( opp, (State(f"{entity_id}", state), ), ) await base_config_test( opp, config, cover_name, COVER_DOMAIN, CONF_COVERS, None, method_discovery=True, ) assert opp.states.get(entity_id).state == state
def _mock_restore_cache(self, temperature=20, operation_mode=STATE_OFF): mock_restore_cache(self.hass, (State( ENTITY, '0', { ATTR_TEMPERATURE: str(temperature), climate.ATTR_OPERATION_MODE: operation_mode, ATTR_AWAY_MODE: "on" }), ))
async def test_restore_state(hass, hvac_mode): """Ensure states are restored on startup.""" mock_restore_cache( hass, (State( "climate.test_thermostat", hvac_mode, { ATTR_TEMPERATURE: "20", ATTR_PRESET_MODE: PRESET_AWAY }, ), ), ) hass.state = CoreState.starting await async_setup_component( hass, DOMAIN, { "climate": { "platform": "generic_thermostat", "name": "test_thermostat", "heater": ENT_SWITCH, "target_sensor": ENT_SENSOR, "away_temp": 14, } }, ) await hass.async_block_till_done() state = hass.states.get("climate.test_thermostat") assert state.attributes[ATTR_TEMPERATURE] == 20 assert state.attributes[ATTR_PRESET_MODE] == PRESET_AWAY assert state.state == hvac_mode
async def test_restore_state(hass: HomeAssistant) -> None: """Test integration sensor state is restored correctly.""" mock_restore_cache( hass, (State( "sensor.integration", "100.0", { "device_class": DEVICE_CLASS_ENERGY, "unit_of_measurement": ENERGY_KILO_WATT_HOUR, }, ), ), ) config = { "sensor": { "platform": "integration", "name": "integration", "source": "sensor.power", "round": 2, } } assert await async_setup_component(hass, "sensor", config) await hass.async_block_till_done() state = hass.states.get("sensor.integration") assert state assert state.state == "100.00" assert state.attributes.get("unit_of_measurement") == ENERGY_KILO_WATT_HOUR assert state.attributes.get("device_class") == DEVICE_CLASS_ENERGY
async def test_restore_state_overrules_initial_state(opp): """Ensure states are restored on startup.""" attr = {"initial": 6, "minimum": 1, "maximum": 8, "step": 2} mock_restore_cache( opp, ( State("counter.test1", "11"), State("counter.test2", "-22"), State("counter.test3", "5", attr), ), ) opp.state = CoreState.starting await async_setup_component( opp, DOMAIN, {DOMAIN: {"test1": {}, "test2": {CONF_INITIAL: 10}, "test3": {}}} ) state = opp.states.get("counter.test1") assert state assert int(state.state) == 11 state = opp.states.get("counter.test2") assert state assert int(state.state) == -22 state = opp.states.get("counter.test3") assert state assert int(state.state) == 5 assert state.attributes.get("initial") == 6 assert state.attributes.get("minimum") == 1 assert state.attributes.get("maximum") == 8 assert state.attributes.get("step") == 2
def _mock_restore_cache(hass, temperature=20, hvac_mode=HVAC_MODE_OFF): mock_restore_cache( hass, (State(ENTITY, hvac_mode, { ATTR_TEMPERATURE: str(temperature), ATTR_PRESET_MODE: PRESET_AWAY }), ))
def test_initial_state_overrules_restore_state(hass): """Ensure states are restored on startup.""" mock_restore_cache(hass, ( State('input_number.b1', '70'), State('input_number.b2', '200'), )) hass.state = CoreState.starting yield from async_setup_component(hass, DOMAIN, { DOMAIN: { 'b1': { 'initial': 50, 'min': 0, 'max': 100, }, 'b2': { 'initial': 60, 'min': 0, 'max': 100, }, }}) state = hass.states.get('input_number.b1') assert state assert float(state.state) == 50 state = hass.states.get('input_number.b2') assert state assert float(state.state) == 60
def test_initial_state_overrules_restore_state(hass): """Ensure states are restored on startup.""" mock_restore_cache(hass, ( State('counter.test1', '11'), State('counter.test2', '-22'), )) hass.state = CoreState.starting yield from async_setup_component(hass, DOMAIN, { DOMAIN: { 'test1': { CONF_RESTORE: False, }, 'test2': { CONF_INITIAL: 10, CONF_RESTORE: False, }, }}) state = hass.states.get('counter.test1') assert state assert int(state.state) == 0 state = hass.states.get('counter.test2') assert state assert int(state.state) == 10
def test_initial_state_overrules_restore_state(hass): """Ensure states are restored on startup.""" mock_restore_cache(hass, ( State('input_text.b1', 'testing'), State('input_text.b2', 'testing too long'), )) hass.state = CoreState.starting yield from async_setup_component(hass, DOMAIN, { DOMAIN: { 'b1': { 'initial': 'test', 'min': 0, 'max': 10, }, 'b2': { 'initial': 'test', 'min': 0, 'max': 10, }, }}) state = hass.states.get('input_text.b1') assert state assert str(state.state) == 'test' state = hass.states.get('input_text.b2') assert state assert str(state.state) == 'test'
def _mock_restore_cache(self, temperature=20, operation_mode=STATE_OFF): mock_restore_cache(self.hass, ( State(ENTITY, '0', { ATTR_TEMPERATURE: str(temperature), climate.ATTR_OPERATION_MODE: operation_mode, ATTR_AWAY_MODE: "on"}), ))
async def test_no_restore_state(hass): """Ensure states are restored on startup if they exist. Allows for graceful reboot. """ mock_restore_cache(hass, ( State('climate.test_thermostat', '0', {ATTR_TEMPERATURE: "20", ATTR_OPERATION_MODE: "off", ATTR_AWAY_MODE: "on"}), )) hass.state = CoreState.starting await async_setup_component( hass, DOMAIN, {'climate': { 'platform': 'generic_thermostat', 'name': 'test_thermostat', 'heater': ENT_SWITCH, 'target_sensor': ENT_SENSOR, 'target_temp': 22 }}) state = hass.states.get('climate.test_thermostat') assert(state.attributes[ATTR_TEMPERATURE] == 22) assert(state.state == STATE_OFF)
def test_no_initial_value_and_restore_off(hass): """Test initial value off and restored state is turned on.""" calls = async_mock_service(hass, 'test', 'automation') mock_restore_cache(hass, ( State('automation.hello', STATE_OFF), )) res = yield from async_setup_component(hass, automation.DOMAIN, { automation.DOMAIN: { 'alias': 'hello', 'trigger': { 'platform': 'event', 'event_type': 'test_event', }, 'action': { 'service': 'test.automation', 'entity_id': 'hello.world' } } }) assert res assert not automation.is_on(hass, 'automation.hello') hass.bus.async_fire('test_event') yield from hass.async_block_till_done() assert len(calls) == 0
def test_async_added_to_hass(hass): """Test restoring state.""" attr = { device_tracker.ATTR_LONGITUDE: 18, device_tracker.ATTR_LATITUDE: -33, device_tracker.ATTR_LATITUDE: -33, device_tracker.ATTR_SOURCE_TYPE: 'gps', device_tracker.ATTR_GPS_ACCURACY: 2, device_tracker.ATTR_BATTERY: 100 } mock_restore_cache(hass, [State('device_tracker.jk', 'home', attr)]) path = hass.config.path(device_tracker.YAML_DEVICES) files = { path: 'jk:\n name: JK Phone\n track: True', } with patch_yaml_files(files): yield from device_tracker.async_setup(hass, {}) state = hass.states.get('device_tracker.jk') assert state assert state.state == 'home' for key, val in attr.items(): atr = state.attributes.get(key) assert atr == val, "{}={} expected: {}".format(key, atr, val)
def test_initial_state_overrules_restore_state(hass): """Ensure states are restored on startup.""" mock_restore_cache(hass, ( State('input_select.s1', 'last option'), State('input_select.s2', 'bad option'), )) options = { 'options': [ 'first option', 'middle option', 'last option', ], 'initial': 'middle option', } yield from async_setup_component(hass, DOMAIN, { DOMAIN: { 's1': options, 's2': options, }}) state = hass.states.get('input_select.s1') assert state assert state.state == 'middle option' state = hass.states.get('input_select.s2') assert state assert state.state == 'middle option'
async def test_restore_state(hass, monkeypatch): """Ensure states are restored on startup.""" config = { 'rflink': { 'port': '/dev/ttyABC0', }, DOMAIN: { 'platform': 'rflink', 'devices': { 'RTS_12345678_0': { 'name': 'c1', }, 'test_restore_2': { 'name': 'c2', }, 'test_restore_3': { 'name': 'c3', }, 'test_restore_4': { 'name': 'c4', }, }, }, } mock_restore_cache(hass, ( State(DOMAIN + '.c1', STATE_OPEN, ), State(DOMAIN + '.c2', STATE_CLOSED, ), )) hass.state = CoreState.starting # setup mocking rflink module _, _, _, _ = await mock_rflink(hass, config, DOMAIN, monkeypatch) state = hass.states.get(DOMAIN + '.c1') assert state assert state.state == STATE_OPEN state = hass.states.get(DOMAIN + '.c2') assert state assert state.state == STATE_CLOSED state = hass.states.get(DOMAIN + '.c3') assert state assert state.state == STATE_CLOSED # not cached cover must default values state = hass.states.get(DOMAIN + '.c4') assert state assert state.state == STATE_CLOSED assert state.attributes['assumed_state']
def test_automation_restore_state(hass): """Ensure states are restored on startup.""" time = dt_util.utcnow() mock_restore_cache(hass, ( State('automation.hello', STATE_ON), State('automation.bye', STATE_OFF, {'last_triggered': time}), )) config = {automation.DOMAIN: [{ 'alias': 'hello', 'trigger': { 'platform': 'event', 'event_type': 'test_event_hello', }, 'action': {'service': 'test.automation'} }, { 'alias': 'bye', 'trigger': { 'platform': 'event', 'event_type': 'test_event_bye', }, 'action': {'service': 'test.automation'} }]} assert (yield from async_setup_component(hass, automation.DOMAIN, config)) state = hass.states.get('automation.hello') assert state assert state.state == STATE_ON state = hass.states.get('automation.bye') assert state assert state.state == STATE_OFF assert state.attributes.get('last_triggered') == time calls = async_mock_service(hass, 'test', 'automation') assert automation.is_on(hass, 'automation.bye') is False hass.bus.async_fire('test_event_bye') yield from hass.async_block_till_done() assert len(calls) == 0 assert automation.is_on(hass, 'automation.hello') hass.bus.async_fire('test_event_hello') yield from hass.async_block_till_done() assert len(calls) == 1
async def test_restore_state(hass, monkeypatch): """Ensure states are restored on startup.""" config = { 'rflink': { 'port': '/dev/ttyABC0', }, DOMAIN: { 'platform': 'rflink', 'devices': { 'test': { 'name': 's1', 'aliases': ['test_alias_0_0'], }, 'switch_test': { 'name': 's2', }, 'switch_s3': { 'name': 's3', } } } } mock_restore_cache(hass, ( State(DOMAIN + '.s1', STATE_ON, ), State(DOMAIN + '.s2', STATE_OFF, ), )) hass.state = CoreState.starting # setup mocking rflink module _, _, _, _ = await mock_rflink(hass, config, DOMAIN, monkeypatch) state = hass.states.get(DOMAIN + '.s1') assert state assert state.state == STATE_ON state = hass.states.get(DOMAIN + '.s2') assert state assert state.state == STATE_OFF # not cached switch must default values state = hass.states.get(DOMAIN + '.s3') assert state assert state.state == STATE_OFF assert state.attributes['assumed_state']
def test_restore_state(hass): """Ensure states are restored on startup.""" mock_restore_cache(hass, ( State('input_datetime.test_time', '19:46:00'), State('input_datetime.test_date', '2017-09-07'), State('input_datetime.test_datetime', '2017-09-07 19:46:00'), State('input_datetime.test_bogus_data', 'this is not a date'), )) hass.state = CoreState.starting initial = datetime.datetime(2017, 1, 1, 23, 42) yield from async_setup_component(hass, DOMAIN, { DOMAIN: { 'test_time': { 'has_time': True, 'has_date': False }, 'test_date': { 'has_time': False, 'has_date': True }, 'test_datetime': { 'has_time': True, 'has_date': True }, 'test_bogus_data': { 'has_time': True, 'has_date': True, 'initial': str(initial) }, }}) dt_obj = datetime.datetime(2017, 9, 7, 19, 46) state_time = hass.states.get('input_datetime.test_time') assert state_time.state == str(dt_obj.time()) state_date = hass.states.get('input_datetime.test_date') assert state_date.state == str(dt_obj.date()) state_datetime = hass.states.get('input_datetime.test_datetime') assert state_datetime.state == str(dt_obj) state_bogus = hass.states.get('input_datetime.test_bogus_data') assert state_bogus.state == str(initial)
def test_restore_state(hass): """Ensure states are restored on startup.""" mock_restore_cache(hass, ( State('climate.test_thermostat', '0', {ATTR_TEMPERATURE: "20", climate.ATTR_OPERATION_MODE: "off"}), )) hass.state = CoreState.starting yield from async_setup_component( hass, climate.DOMAIN, {'climate': { 'platform': 'generic_thermostat', 'name': 'test_thermostat', 'heater': ENT_SWITCH, 'target_sensor': ENT_SENSOR, }}) state = hass.states.get('climate.test_thermostat') assert(state.attributes[ATTR_TEMPERATURE] == 20) assert(state.attributes[climate.ATTR_OPERATION_MODE] == "off")
async def test_restore_disarmed_state(hass): """Ensure disarmed state is restored on startup.""" mock_restore_cache(hass, ( State('alarm_control_panel.test', STATE_ALARM_DISARMED), )) hass.state = CoreState.starting mock_component(hass, 'recorder') assert await async_setup_component(hass, alarm_control_panel.DOMAIN, { 'alarm_control_panel': { 'platform': 'manual', 'name': 'test', 'pending_time': 0, 'trigger_time': 0, 'disarm_after_trigger': False }}) state = hass.states.get('alarm_control_panel.test') assert state assert state.state == STATE_ALARM_DISARMED
def test_initial_state_overrules_restore_state(hass): """Ensure states are restored on startup.""" mock_restore_cache(hass, ( State('input_boolean.b1', 'on'), State('input_boolean.b2', 'off'), )) hass.state = CoreState.starting yield from async_setup_component(hass, DOMAIN, { DOMAIN: { 'b1': {CONF_INITIAL: False}, 'b2': {CONF_INITIAL: True}, }}) state = hass.states.get('input_boolean.b1') assert state assert state.state == 'off' state = hass.states.get('input_boolean.b2') assert state assert state.state == 'on'
def test_no_restore_state(hass): """Ensure states are not restored on startup if not needed.""" mock_restore_cache(hass, ( State('climate.test_thermostat', '0', {ATTR_TEMPERATURE: "20", climate.ATTR_OPERATION_MODE: "off"}), )) hass.state = CoreState.starting yield from async_setup_component( hass, climate.DOMAIN, {'climate': { 'platform': 'generic_thermostat', 'name': 'test_thermostat', 'heater': ENT_SWITCH, 'target_sensor': ENT_SENSOR, 'target_temp': 22, 'initial_operation_mode': 'auto', }}) state = hass.states.get('climate.test_thermostat') assert(state.attributes[ATTR_TEMPERATURE] == 22) assert(state.attributes[climate.ATTR_OPERATION_MODE] != "off")
async def test_restore_home_state(hass, hass_admin_user): """Test that the state is restored for a person on startup.""" user_id = hass_admin_user.id attrs = { ATTR_ID: '1234', ATTR_LATITUDE: 10.12346, ATTR_LONGITUDE: 11.12346, ATTR_SOURCE: DEVICE_TRACKER, ATTR_USER_ID: user_id} state = State('person.tracked_person', 'home', attrs) mock_restore_cache(hass, (state, )) hass.state = CoreState.not_running mock_component(hass, 'recorder') config = {DOMAIN: { 'id': '1234', 'name': 'tracked person', 'user_id': user_id, 'device_trackers': DEVICE_TRACKER}} assert await async_setup_component(hass, DOMAIN, config) state = hass.states.get('person.tracked_person') assert state.state == 'home' assert state.attributes.get(ATTR_ID) == '1234' assert state.attributes.get(ATTR_LATITUDE) == 10.12346 assert state.attributes.get(ATTR_LONGITUDE) == 11.12346 # When restoring state the entity_id of the person will be used as source. assert state.attributes.get(ATTR_SOURCE) == 'person.tracked_person' assert state.attributes.get(ATTR_USER_ID) == user_id
def test_restore_state(hass): """Ensure states are restored on startup.""" mock_restore_cache(hass, ( State('input_boolean.b1', 'on'), State('input_boolean.b2', 'off'), State('input_boolean.b3', 'on'), )) hass.state = CoreState.starting mock_component(hass, 'recorder') yield from async_setup_component(hass, DOMAIN, { DOMAIN: { 'b1': None, 'b2': None, }}) state = hass.states.get('input_boolean.b1') assert state assert state.state == 'on' state = hass.states.get('input_boolean.b2') assert state assert state.state == 'off'
async def test_restore_state(hass, monkeypatch): """Ensure states are restored on startup.""" config = { 'rflink': { 'port': '/dev/ttyABC0', }, DOMAIN: { 'platform': 'rflink', 'devices': { 'NewKaku_12345678_0': { 'name': 'l1', 'type': 'hybrid', }, 'test_restore_2': { 'name': 'l2', }, 'test_restore_3': { 'name': 'l3', }, 'test_restore_4': { 'name': 'l4', 'type': 'dimmable', }, 'test_restore_5': { 'name': 'l5', 'type': 'dimmable', }, }, }, } mock_restore_cache(hass, ( State(DOMAIN + '.l1', STATE_ON, {ATTR_BRIGHTNESS: "123", }), State(DOMAIN + '.l2', STATE_ON, {ATTR_BRIGHTNESS: "321", }), State(DOMAIN + '.l3', STATE_OFF, ), State(DOMAIN + '.l5', STATE_ON, {ATTR_BRIGHTNESS: "222", }), )) hass.state = CoreState.starting # setup mocking rflink module _, _, _, _ = await mock_rflink(hass, config, DOMAIN, monkeypatch) # hybrid light must restore brightness state = hass.states.get(DOMAIN + '.l1') assert state assert state.state == STATE_ON assert state.attributes[ATTR_BRIGHTNESS] == 123 # normal light do NOT must restore brightness state = hass.states.get(DOMAIN + '.l2') assert state assert state.state == STATE_ON assert not state.attributes.get(ATTR_BRIGHTNESS) # OFF state also restores (or not) state = hass.states.get(DOMAIN + '.l3') assert state assert state.state == STATE_OFF # not cached light must default values state = hass.states.get(DOMAIN + '.l4') assert state assert state.state == STATE_OFF assert not state.attributes.get(ATTR_BRIGHTNESS) assert state.attributes['assumed_state'] # test coverage for dimmable light state = hass.states.get(DOMAIN + '.l5') assert state assert state.state == STATE_ON assert state.attributes[ATTR_BRIGHTNESS] == 222