示例#1
0
    def async_added_to_hass(self) -> None:
        """Startup with initial state or previous state."""
        enable_automation = False

        state = yield from async_get_last_state(self.hass, self.entity_id)
        if state is None:
            if self._initial_state:
                enable_automation = True
        else:
            self._last_triggered = state.attributes.get('last_triggered')
            if state.state == STATE_ON:
                enable_automation = True

        # HomeAssistant is on bootstrap
        if enable_automation and self.hass.state == CoreState.not_running:

            @asyncio.coroutine
            def async_enable_automation(event):
                """Start automation on startup."""
                yield from self.async_enable()

            self.hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START,
                                            async_enable_automation)

        # HomeAssistant is running
        elif enable_automation:
            yield from self.async_enable()
 def async_added_to_hass(self):
     """Run when entity about to be added."""
     # Check If we have an old state
     old_state = yield from async_get_last_state(self.hass, self.entity_id)
     if old_state is not None:
         # If we have no initial temperature, restore
         if self._target_temp is None:
             # If we have a previously saved temperature
             if old_state.attributes[ATTR_TEMPERATURE] is None:
                 if self.ac_mode:
                     self._target_temp = self.max_temp
                 else:
                     self._target_temp = self.min_temp
                 _LOGGER.warning(
                     'Undefined target temperature, \
                                 falling back to %s', self._target_temp)
             else:
                 self._target_temp = float(
                     old_state.attributes[ATTR_TEMPERATURE])
         self._is_away = True if str(
             old_state.attributes[ATTR_AWAY_MODE]) == STATE_ON else False
         if old_state.attributes[ATTR_OPERATION_MODE] == STATE_OFF:
             self._current_operation = STATE_OFF
             self._enabled = False
         if self._initial_operation_mode is None:
             if old_state.attributes[ATTR_OPERATION_MODE] == STATE_OFF:
                 self._enabled = False
示例#3
0
def test_caching_data(hass):
    """Test that we cache data."""
    mock_component(hass, 'recorder')
    hass.state = CoreState.starting

    states = [
        State('input_boolean.b0', 'on'),
        State('input_boolean.b1', 'on'),
        State('input_boolean.b2', 'on'),
    ]

    with patch('homeassistant.helpers.restore_state.last_recorder_run',
               return_value=MagicMock(end=dt_util.utcnow())), \
            patch('homeassistant.helpers.restore_state.get_states',
                  return_value=states), \
            patch('homeassistant.helpers.restore_state.wait_connection_ready',
                  return_value=mock_coro(True)):
        state = yield from async_get_last_state(hass, 'input_boolean.b1')

    assert DATA_RESTORE_CACHE in hass.data
    assert hass.data[DATA_RESTORE_CACHE] == {st.entity_id: st for st in states}

    assert state is not None
    assert state.entity_id == 'input_boolean.b1'
    assert state.state == 'on'

    hass.bus.async_fire(EVENT_HOMEASSISTANT_START)

    yield from hass.async_block_till_done()

    assert DATA_RESTORE_CACHE not in hass.data
示例#4
0
    def async_added_to_hass(self) -> None:
        """Startup with initial state or previous state."""
        enable_automation = DEFAULT_INITIAL_STATE

        if self._initial_state is not None:
            enable_automation = self._initial_state
        else:
            state = yield from async_get_last_state(self.hass, self.entity_id)
            if state:
                enable_automation = state.state == STATE_ON
                self._last_triggered = state.attributes.get('last_triggered')

        if not enable_automation:
            return

        # HomeAssistant is starting up
        elif self.hass.state == CoreState.not_running:

            @asyncio.coroutine
            def async_enable_automation(event):
                """Start automation on startup."""
                yield from self.async_enable()

            self.hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START,
                                            async_enable_automation)

        # HomeAssistant is running
        else:
            yield from self.async_enable()
def test_caching_data(hass):
    """Test that we cache data."""
    mock_component(hass, 'recorder')
    hass.state = CoreState.starting

    states = [
        State('input_boolean.b0', 'on'),
        State('input_boolean.b1', 'on'),
        State('input_boolean.b2', 'on'),
    ]

    with patch('homeassistant.helpers.restore_state.last_recorder_run',
               return_value=MagicMock(end=dt_util.utcnow())), \
            patch('homeassistant.helpers.restore_state.get_states',
                  return_value=states), \
            patch('homeassistant.helpers.restore_state.wait_connection_ready',
                  return_value=mock_coro(True)):
        state = yield from async_get_last_state(hass, 'input_boolean.b1')

    assert DATA_RESTORE_CACHE in hass.data
    assert hass.data[DATA_RESTORE_CACHE] == {st.entity_id: st for st in states}

    assert state is not None
    assert state.entity_id == 'input_boolean.b1'
    assert state.state == 'on'

    hass.bus.async_fire(EVENT_HOMEASSISTANT_START)

    yield from hass.async_block_till_done()

    assert DATA_RESTORE_CACHE not in hass.data
示例#6
0
    def async_added_to_hass(self):
        state = yield from async_get_last_state(self.hass, self.entity_id)

        if state is not None:
            self._target_temperature = state.attributes['temperature']
            self._current_operation = state.attributes['operation_mode']
            self._current_fan_mode = state.attributes['fan_mode']
示例#7
0
 def async_added_to_hass(self):
     """Handle entity which will be added."""
     state = yield from async_get_last_state(self.hass, self.entity_id)
     if state:
         for attr in iter(state.attributes):
             if attr != ATTR_FRIENDLY_NAME:
                 _LOGGER.info('{}: set attribute {} from last state: {}'.format(self._name, attr, state.attributes[attr]))
                 self.update_attributes(attr, state.attributes[attr])
示例#8
0
    def async_added_to_hass(self):
        """Call when entity about to be added to hass."""
        # If not None, we got an initial value.
        if self._state is not None:
            return

        state = yield from async_get_last_state(self.hass, self.entity_id)
        self._state = state and state.state == STATE_ON
示例#9
0
 def async_added_to_hass(self):
     """Called when entity is about to be added to hass."""
     last_state = yield from async_get_last_state(self.hass, self.entity_id)
     if last_state:
         self._is_on = (last_state.state == STATE_ON)
         self._brightness = last_state.attributes.get('brightness')
         self._temperature = last_state.attributes.get('color_temp')
         self._color = last_state.attributes.get('hs_color')
示例#10
0
    def async_added_to_hass(self):
        """Call when entity about to be added to Home Assistant."""
        # If not None, we got an initial value.
        if self._state is not None:
            return

        state = yield from async_get_last_state(self.hass, self.entity_id)
        self._state = state and state.state == state
示例#11
0
 def async_added_to_hass(self):
     """Called when entity is about to be added to hass."""
     last_state = yield from async_get_last_state(self.hass, self.entity_id)
     if last_state:
         self._is_on = (last_state.state == STATE_ON)
         self._brightness = last_state.attributes.get('brightness')
         self._temperature = last_state.attributes.get('color_temp')
         self._color = last_state.attributes.get('hs_color')
示例#12
0
 def async_added_to_hass(self):
     """Called when entity about to be added to hass."""
     state = yield from async_get_last_state(self.hass, self.entity_id)
     if not state:
         return
     if state.state not in self._options:
         return
     self._current_option = state.state
 def async_added_to_hass(self):
     """Run when entity about to be added."""
     # If we have an old state and no target temp, restore
     if self._target_temp is None:
         old_state = yield from async_get_last_state(self.hass,
                                                     self.entity_id)
         if old_state is not None:
             self._target_temp = float(
                 old_state.attributes[ATTR_TEMPERATURE])
示例#14
0
 def async_added_to_hass(self) -> None:
     """Startup with initial state or previous state."""
     state = yield from async_get_last_state(self.hass, self.entity_id)
     if state is None:
         if self._initial_state:
             yield from self.async_enable()
     else:
         self._last_triggered = state.attributes.get('last_triggered')
         if state.state == STATE_ON:
             yield from self.async_enable()
示例#15
0
 def async_added_to_hass(self) -> None:
     """Startup with initial state or previous state."""
     state = yield from async_get_last_state(self.hass, self.entity_id)
     if state is None:
         if self._initial_state:
             yield from self.async_enable()
     else:
         self._last_triggered = state.attributes.get('last_triggered')
         if state.state == STATE_ON:
             yield from self.async_enable()
示例#16
0
    def async_added_to_hass(self):
        """Run when entity about to be added."""
        if self._current_option is not None:
            return

        state = yield from async_get_last_state(self.hass, self.entity_id)
        if not state or state.state not in self._options:
            self._current_option = self._options[0]
        else:
            self._current_option = state.state
示例#17
0
    def async_added_to_hass(self):
        """Run when entity about to be added."""
        if self._current_option is not None:
            return

        state = yield from async_get_last_state(self.hass, self.entity_id)
        if not state or state.state not in self._options:
            self._current_option = self._options[0]
        else:
            self._current_option = state.state
示例#18
0
    def async_added_to_hass(self):
        """Called when entity about to be added to hass."""
        state = yield from async_get_last_state(self.hass, self.entity_id)
        if not state:
            return

        num_value = float(state.state)
        if num_value < self._minimum or num_value > self._maximum:
            return
        self._current_value = num_value
示例#19
0
    def async_added_to_hass(self):
        """Run when entity about to be added to hass."""
        if self._current_value is not None:
            return

        state = yield from async_get_last_state(self.hass, self.entity_id)
        value = state and state.state

        # Check against None because value can be 0
        if value is not None and self._minimum <= len(value) <= self._maximum:
            self._current_value = value
示例#20
0
    def async_added_to_hass(self):
        """Run when entity about to be added to hass."""
        if self._current_value is not None:
            return

        state = yield from async_get_last_state(self.hass, self.entity_id)
        value = state and state.state

        # Check against None because value can be 0
        if value is not None and self._minimum <= len(value) <= self._maximum:
            self._current_value = value
示例#21
0
 def async_added_to_hass(self):
     """Run when entity about to be added."""
     old_state = yield from async_get_last_state(self.hass, self.entity_id)
     if old_state is not None:
         _LOGGER.debug("Loading %s old_state: %s",
                       self.entity_id, old_state)
         if old_state.attributes.get(ATTR_CURRENT_TEMPERATURE):
             self._current_temperature = float(
                 old_state.attributes[ATTR_CURRENT_TEMPERATURE])
         if old_state.attributes.get(ATTR_CURRENT_HUMIDITY):
             self._current_humidity = int(
                 old_state.attributes[ATTR_CURRENT_HUMIDITY])
示例#22
0
 def async_added_to_hass(self):
     """Run when entity about to be added."""
     old_state = yield from async_get_last_state(self.hass, self.entity_id)
     if old_state is not None:
         if old_state.attributes.get(ATTR_HOMEGW_TEMPERATURE):
             self._temperature = float(
                 old_state.attributes[ATTR_HOMEGW_TEMPERATURE])
         if old_state.attributes.get(ATTR_HOMEGW_HUMIDITY):
             self._humidity = int(
                 old_state.attributes[ATTR_HOMEGW_HUMIDITY])
         if old_state.attributes.get(ATTR_HOMEGW_PRESSURE):
             self._pressure = int(
                 old_state.attributes[ATTR_HOMEGW_PRESSURE])
示例#23
0
    def async_added_to_hass(self):
        """Called when entity about to be added to hass."""
        if self._current_value is not None:
            return

        state = yield from async_get_last_state(self.hass, self.entity_id)
        value = state and float(state.state)

        # Check against False because value can be 0
        if value is not False and self._minimum < value < self._maximum:
            self._current_value = value
        else:
            self._current_value = self._minimum
示例#24
0
    def async_added_to_hass(self):
        """Run when entity about to be added."""
        # Check If we have an old state
        old_state = yield from async_get_last_state(self.hass, self.entity_id)
        if old_state is not None:
            # If we have no initial temperature, restore
            if self._target_temp is None:
                self._target_temp = float(
                    old_state.attributes[ATTR_TEMPERATURE])

            # If we have no initial operation mode, restore
            if self._initial_operation_mode is None:
                if old_state.attributes[ATTR_OPERATION_MODE] == STATE_OFF:
                    self._enabled = False
示例#25
0
    def async_added_to_hass(self):
        state = yield from async_get_last_state(self.hass, self.entity_id)

        if state is not None:
            self._current_operation = state.attributes.get(
                ATTR_OPERATION_MODE, self._current_operation)
            self._target_temperature = state.attributes.get(
                ATTR_TEMPERATURE, self._target_temperature)
            self._enabled_flags = state.attributes.get(ATTR_SUPPORTED_FEATURES,
                                                       self._enabled_flags)
            self._current_fan_mode = state.attributes.get(
                ATTR_FAN_MODE, self._current_fan_mode)
            self._on = state.attributes.get(ATTR_POWER, STATE_OFF) == STATE_ON
            self._away = state.attributes.get(ATTR_AWAY_MODE,
                                              STATE_OFF) == STATE_ON
示例#26
0
def test_no_last_run_found(hass):
    """Test that cache cannot be accessed if no last run found."""
    mock_component(hass, 'recorder')
    hass.state = CoreState.starting

    states = [State('input_boolean.b1', 'on')]

    with patch('homeassistant.helpers.restore_state.last_recorder_run',
               return_value=None), \
            patch('homeassistant.helpers.restore_state.get_states',
                  return_value=states), \
            patch('homeassistant.helpers.restore_state.wait_connection_ready',
                  return_value=mock_coro(True)):
        state = yield from async_get_last_state(hass, 'input_boolean.b1')
    assert state is None
示例#27
0
def test_not_connected(hass):
    """Test that cache cannot be accessed if db connection times out."""
    mock_component(hass, 'recorder')
    hass.state = CoreState.starting

    states = [State('input_boolean.b1', 'on')]

    with patch('homeassistant.helpers.restore_state.last_recorder_run',
               return_value=MagicMock(end=dt_util.utcnow())), \
            patch('homeassistant.helpers.restore_state.get_states',
                  return_value=states), \
            patch('homeassistant.helpers.restore_state.wait_connection_ready',
                  return_value=mock_coro(False)):
        state = yield from async_get_last_state(hass, 'input_boolean.b1')
    assert state is None
    def async_added_to_hass(self):
        """Run when entity about to be added."""
        # Check If we have an old state
        old_state = yield from async_get_last_state(self.hass,
                                                    self.entity_id)
        if old_state is not None:
            # If we have no initial temperature, restore
            if self._target_temp is None:
                self._target_temp = float(
                    old_state.attributes[ATTR_TEMPERATURE])

            # If we have no initial operation mode, restore
            if self._initial_operation_mode is None:
                if old_state.attributes[ATTR_OPERATION_MODE] == STATE_OFF:
                    self._enabled = False
def test_not_connected(hass):
    """Test that cache cannot be accessed if db connection times out."""
    mock_component(hass, 'recorder')
    hass.state = CoreState.starting

    states = [State('input_boolean.b1', 'on')]

    with patch('homeassistant.helpers.restore_state.last_recorder_run',
               return_value=MagicMock(end=dt_util.utcnow())), \
            patch('homeassistant.helpers.restore_state.get_states',
                  return_value=states), \
            patch('homeassistant.helpers.restore_state.wait_connection_ready',
                  return_value=mock_coro(False)):
        state = yield from async_get_last_state(hass, 'input_boolean.b1')
    assert state is None
def test_no_last_run_found(hass):
    """Test that cache cannot be accessed if no last run found."""
    mock_component(hass, 'recorder')
    hass.state = CoreState.starting

    states = [State('input_boolean.b1', 'on')]

    with patch('homeassistant.helpers.restore_state.last_recorder_run',
               return_value=None), \
            patch('homeassistant.helpers.restore_state.get_states',
                  return_value=states), \
            patch('homeassistant.helpers.restore_state.wait_connection_ready',
                  return_value=mock_coro(True)):
        state = yield from async_get_last_state(hass, 'input_boolean.b1')
    assert state is None
示例#31
0
    def async_added_to_hass(self) -> None:
        """Startup with initial state or previous state."""
        if self._initial_state is not None:
            enable_automation = self._initial_state
            _LOGGER.debug(
                "Automation %s initial state %s from config "
                "initial_state", self.entity_id, enable_automation)
        else:
            state = yield from async_get_last_state(self.hass, self.entity_id)
            if state:
                enable_automation = state.state == STATE_ON
                last_triggered = state.attributes.get('last_triggered')
                if last_triggered is not None:
                    if isinstance(last_triggered, str):
                        self._last_triggered = parse_datetime(last_triggered)
                    else:
                        self._last_triggered = last_triggered
                else:
                    self._last_triggered = last_triggered
                _LOGGER.debug(
                    "Automation %s initial state %s from recorder "
                    "last state %s", self.entity_id, enable_automation, state)
            else:
                enable_automation = DEFAULT_INITIAL_STATE
                _LOGGER.debug(
                    "Automation %s initial state %s from default "
                    "initial state", self.entity_id, enable_automation)

        if not enable_automation:
            return

        # HomeAssistant is starting up
        if self.hass.state == CoreState.not_running:

            @asyncio.coroutine
            def async_enable_automation(event):
                """Start automation on startup."""
                yield from self.async_enable()

            self.hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START,
                                            async_enable_automation)

        # HomeAssistant is running
        else:
            yield from self.async_enable()
def test_hass_running(hass):
    """Test that cache cannot be accessed while hass is running."""
    mock_component(hass, 'recorder')

    states = [
        State('input_boolean.b0', 'on'),
        State('input_boolean.b1', 'on'),
        State('input_boolean.b2', 'on'),
    ]

    with patch('homeassistant.helpers.restore_state.last_recorder_run',
               return_value=MagicMock(end=dt_util.utcnow())), \
            patch('homeassistant.helpers.restore_state.get_states',
                  return_value=states), \
            patch('homeassistant.helpers.restore_state.wait_connection_ready',
                  return_value=mock_coro(True)):
        state = yield from async_get_last_state(hass, 'input_boolean.b1')
    assert state is None
示例#33
0
    def async_added_to_hass(self):
        """Add an entity."""
        state = yield from async_get_last_state(self.hass, self.entity_id)
        if not state:
            return
        self._state = state.state

        for attr, var in (
            (ATTR_SOURCE_TYPE, 'source_type'),
            (ATTR_GPS_ACCURACY, 'gps_accuracy'),
            (ATTR_BATTERY, 'battery'),
        ):
            if attr in state.attributes:
                setattr(self, var, state.attributes[attr])

        if ATTR_LONGITUDE in state.attributes:
            self.gps = (state.attributes[ATTR_LATITUDE],
                        state.attributes[ATTR_LONGITUDE])
示例#34
0
def test_hass_running(hass):
    """Test that cache cannot be accessed while hass is running."""
    mock_component(hass, 'recorder')

    states = [
        State('input_boolean.b0', 'on'),
        State('input_boolean.b1', 'on'),
        State('input_boolean.b2', 'on'),
    ]

    with patch('homeassistant.helpers.restore_state.last_recorder_run',
               return_value=MagicMock(end=dt_util.utcnow())), \
            patch('homeassistant.helpers.restore_state.get_states',
                  return_value=states), \
            patch('homeassistant.helpers.restore_state.wait_connection_ready',
                  return_value=mock_coro(True)):
        state = yield from async_get_last_state(hass, 'input_boolean.b1')
    assert state is None
示例#35
0
    def async_added_to_hass(self):
        """Add an entity."""
        state = yield from async_get_last_state(self.hass, self.entity_id)
        if not state:
            return
        self._state = state.state

        for attr, var in (
                (ATTR_SOURCE_TYPE, 'source_type'),
                (ATTR_GPS_ACCURACY, 'gps_accuracy'),
                (ATTR_BATTERY, 'battery'),
        ):
            if attr in state.attributes:
                setattr(self, var, state.attributes[attr])

        if ATTR_LONGITUDE in state.attributes:
            self.gps = (state.attributes[ATTR_LATITUDE],
                        state.attributes[ATTR_LONGITUDE])
def test_cache_timeout(hass):
    """Test that cache timeout returns none."""
    mock_component(hass, 'recorder')
    hass.state = CoreState.starting

    states = [State('input_boolean.b1', 'on')]

    @asyncio.coroutine
    def timeout_coro():
        raise asyncio.TimeoutError()

    with patch('homeassistant.helpers.restore_state.last_recorder_run',
               return_value=MagicMock(end=dt_util.utcnow())), \
            patch('homeassistant.helpers.restore_state.get_states',
                  return_value=states), \
            patch('homeassistant.helpers.restore_state.wait_connection_ready',
                  return_value=timeout_coro()):
        state = yield from async_get_last_state(hass, 'input_boolean.b1')
    assert state is None
示例#37
0
def test_cache_timeout(hass):
    """Test that cache timeout returns none."""
    mock_component(hass, 'recorder')
    hass.state = CoreState.starting

    states = [State('input_boolean.b1', 'on')]

    @asyncio.coroutine
    def timeout_coro():
        raise asyncio.TimeoutError()

    with patch('homeassistant.helpers.restore_state.last_recorder_run',
               return_value=MagicMock(end=dt_util.utcnow())), \
            patch('homeassistant.helpers.restore_state.get_states',
                  return_value=states), \
            patch('homeassistant.helpers.restore_state.wait_connection_ready',
                  return_value=timeout_coro()):
        state = yield from async_get_last_state(hass, 'input_boolean.b1')
    assert state is None
示例#38
0
    def async_added_to_hass(self):
        """Register callbacks."""
        state = yield from async_get_last_state(self.hass, self.entity_id)
        if state:
            self._state = state.state == STATE_ON

        @callback
        def template_bsensor_state_listener(entity, old_state, new_state):
            """Called when the target device changes state."""
            self.hass.async_add_job(self.async_update_ha_state(True))

        @callback
        def template_bsensor_startup(event):
            """Update template on startup."""
            async_track_state_change(self.hass, self._entities,
                                     template_bsensor_state_listener)

            self.hass.async_add_job(self.async_update_ha_state(True))

        self.hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START,
                                        template_bsensor_startup)
示例#39
0
    def async_added_to_hass(self):
        """Register callbacks."""
        state = yield from async_get_last_state(self.hass, self.entity_id)
        if state:
            self._position = 100 if state.state == STATE_OPEN else 0

        @callback
        def template_cover_state_listener(entity, old_state, new_state):
            """Handle target device state changes."""
            self.async_schedule_update_ha_state(True)

        @callback
        def template_cover_startup(event):
            """Update template on startup."""
            async_track_state_change(
                self.hass, self._entities, template_cover_state_listener)

            self.async_schedule_update_ha_state(True)

        self.hass.bus.async_listen_once(
            EVENT_HOMEASSISTANT_START, template_cover_startup)
    def async_added_to_hass(self):
        """Register callbacks."""
        state = yield from async_get_last_state(self.hass, self.entity_id)
        if state:
            self._state = state.state

        @callback
        def template_sensor_state_listener(entity, old_state, new_state):
            """Handle device state changes."""
            self.async_schedule_update_ha_state(True)

        @callback
        def template_sensor_startup(event):
            """Update template on startup."""
            async_track_state_change(self.hass, self._entities,
                                     template_sensor_state_listener)

            self.async_schedule_update_ha_state(True)

        self.hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START,
                                        template_sensor_startup)
示例#41
0
    def async_added_to_hass(self):
        """Register callbacks."""
        state = yield from async_get_last_state(self.hass, self.entity_id)
        if state:
            self._state = state.state

        @callback
        def template_sensor_state_listener(entity, old_state, new_state):
            """Handle device state changes."""
            self.hass.async_add_job(self.async_update_ha_state(True))

        @callback
        def template_sensor_startup(event):
            """Update template on startup."""
            async_track_state_change(
                self.hass, self._entities, template_sensor_state_listener)

            self.hass.async_add_job(self.async_update_ha_state(True))

        self.hass.bus.async_listen_once(
            EVENT_HOMEASSISTANT_START, template_sensor_startup)
示例#42
0
    def async_added_to_hass(self):
        """Register callbacks."""
        state = yield from async_get_last_state(self.hass, self.entity_id)
        if state:
            self._position = 100 if state.state == STATE_OPEN else 0

        @callback
        def template_cover_state_listener(entity, old_state, new_state):
            """Handle target device state changes."""
            self.hass.async_add_job(self.async_update_ha_state(True))

        @callback
        def template_cover_startup(event):
            """Update template on startup."""
            async_track_state_change(
                self.hass, self._entities, template_cover_state_listener)

            self.hass.async_add_job(self.async_update_ha_state(True))

        self.hass.bus.async_listen_once(
            EVENT_HOMEASSISTANT_START, template_cover_startup)
    def async_added_to_hass(self):
        """Run when entity about to be added."""
        restore_val = None

        # Priority 1: Initial State
        if self._initial is not None:
            restore_val = self._initial

        # Priority 2: Old state
        if restore_val is None:
            old_state = yield from async_get_last_state(self.hass,
                                                        self.entity_id)
            if old_state is not None:
                restore_val = old_state.state

        if restore_val is not None:
            if not self._has_date:
                self._current_datetime = dt_util.parse_time(restore_val)
            elif not self._has_time:
                self._current_datetime = dt_util.parse_date(restore_val)
            else:
                self._current_datetime = dt_util.parse_datetime(restore_val)
示例#44
0
    def async_added_to_hass(self):
        """Run when entity about to be added."""
        restore_val = None

        # Priority 1: Initial State
        if self._initial is not None:
            restore_val = self._initial

        # Priority 2: Old state
        if restore_val is None:
            old_state = yield from async_get_last_state(
                self.hass, self.entity_id)
            if old_state is not None:
                restore_val = old_state.state

        if restore_val is not None:
            if not self._has_date:
                self._current_datetime = dt_util.parse_time(restore_val)
            elif not self._has_time:
                self._current_datetime = dt_util.parse_date(restore_val)
            else:
                self._current_datetime = dt_util.parse_datetime(restore_val)
    def async_added_to_hass(self):
        """Run when entity about to be added."""
        # Check If we have an old state
        old_state = yield from async_get_last_state(self.hass,
                                                    self.entity_id)
        if old_state is not None:
            # If we have no initial temperature, restore
            if self._target_temp is None:
                # If we have a previously saved temperature
                if old_state.attributes.get(ATTR_TEMPERATURE) is None:
                    if self.ac_mode:
                        self._target_temp = self.max_temp
                    else:
                        self._target_temp = self.min_temp
                    _LOGGER.warning("Undefined target temperature,"
                                    "falling back to %s", self._target_temp)
                else:
                    self._target_temp = float(
                        old_state.attributes[ATTR_TEMPERATURE])
            if old_state.attributes.get(ATTR_AWAY_MODE) is not None:
                self._is_away = str(
                    old_state.attributes[ATTR_AWAY_MODE]) == STATE_ON
            if (self._initial_operation_mode is None and
                    old_state.attributes[ATTR_OPERATION_MODE] is not None):
                self._current_operation = \
                    old_state.attributes[ATTR_OPERATION_MODE]
                self._enabled = self._current_operation != STATE_OFF

        else:
            # No previous state, try and restore defaults
            if self._target_temp is None:
                if self.ac_mode:
                    self._target_temp = self.max_temp
                else:
                    self._target_temp = self.min_temp
            _LOGGER.warning("No previously saved temperature, setting to %s",
                            self._target_temp)
    def async_added_to_hass(self):
        """Run when entity about to be added."""
        # Check If we have an old state
        old_state = yield from async_get_last_state(self.hass, self.entity_id)
        if old_state is not None:
            # If we have no initial temperature, restore
            if self._target_temp is None:
                # If we have a previously saved temperature
                if old_state.attributes.get(ATTR_TEMPERATURE) is None:
                    if self.ac_mode:
                        self._target_temp = self.max_temp
                    else:
                        self._target_temp = self.min_temp
                    _LOGGER.warning(
                        "Undefined target temperature,"
                        "falling back to %s", self._target_temp)
                else:
                    self._target_temp = float(
                        old_state.attributes[ATTR_TEMPERATURE])
            if old_state.attributes.get(ATTR_AWAY_MODE) is not None:
                self._is_away = str(
                    old_state.attributes[ATTR_AWAY_MODE]) == STATE_ON
            if (self._initial_operation_mode is None
                    and old_state.attributes[ATTR_OPERATION_MODE] is not None):
                self._current_operation = \
                    old_state.attributes[ATTR_OPERATION_MODE]
                self._enabled = self._current_operation != STATE_OFF

        else:
            # No previous state, try and restore defaults
            if self._target_temp is None:
                if self.ac_mode:
                    self._target_temp = self.max_temp
                else:
                    self._target_temp = self.min_temp
            _LOGGER.warning("No previously saved temperature, setting to %s",
                            self._target_temp)
示例#47
0
    def async_added_to_hass(self) -> None:
        """Startup with initial state or previous state."""
        if self._initial_state is not None:
            enable_automation = self._initial_state
            _LOGGER.debug("Automation %s initial state %s from config "
                          "initial_state", self.entity_id, enable_automation)
        else:
            state = yield from async_get_last_state(self.hass, self.entity_id)
            if state:
                enable_automation = state.state == STATE_ON
                self._last_triggered = state.attributes.get('last_triggered')
                _LOGGER.debug("Automation %s initial state %s from recorder "
                              "last state %s", self.entity_id,
                              enable_automation, state)
            else:
                enable_automation = DEFAULT_INITIAL_STATE
                _LOGGER.debug("Automation %s initial state %s from default "
                              "initial state", self.entity_id,
                              enable_automation)

        if not enable_automation:
            return

        # HomeAssistant is starting up
        elif self.hass.state == CoreState.not_running:
            @asyncio.coroutine
            def async_enable_automation(event):
                """Start automation on startup."""
                yield from self.async_enable()

            self.hass.bus.async_listen_once(
                EVENT_HOMEASSISTANT_START, async_enable_automation)

        # HomeAssistant is running
        else:
            yield from self.async_enable()
示例#48
0
 def async_added_to_hass(self):
     """Handle entity which will be added."""
     state = yield from async_get_last_state(self.hass, self.entity_id)
     if not state:
         return
     self._state = state.state
示例#49
0
 def async_added_to_hass(self):
     """Call when entity about to be added to hass."""
     state = yield from async_get_last_state(self._hass, self.entity_id)
     if state:
         self._state = state.state == STATE_ON
示例#50
0
 def async_added_to_hass(self):
     """Called when entity is about to be added to hass."""
     state = yield from async_get_last_state(self.hass, self.entity_id)
     if not state:
         return
     self._state = state.state