Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initialise plant attributes at startup #19315

Merged
merged 3 commits into from
Dec 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions homeassistant/components/plant.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,6 @@ async def async_setup(hass, config):
for plant_name, plant_config in config[DOMAIN].items():
_LOGGER.info("Added plant %s", plant_name)
entity = Plant(plant_name, plant_config)
sensor_entity_ids = list(plant_config[CONF_SENSORS].values())
_LOGGER.debug("Subscribing to entity_ids %s", sensor_entity_ids)
async_track_state_change(hass, sensor_entity_ids, entity.state_changed)
entities.append(entity)

await component.async_add_entities(entities)
Expand Down Expand Up @@ -250,6 +247,14 @@ async def async_added_to_hass(self):
# only use the database if it's configured
self.hass.async_add_job(self._load_history_from_db)

async_track_state_change(self.hass, list(self._sensormap),
self.state_changed)

for entity_id in self._sensormap:
state = self.hass.states.get(entity_id)
if state is not None:
self.state_changed(entity_id, None, state)
MartinHjelmare marked this conversation as resolved.
Show resolved Hide resolved

async def _load_history_from_db(self):
"""Load the history of the brightness values from the database.

Expand Down
14 changes: 14 additions & 0 deletions tests/components/test_plant.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,20 @@ def test_low_battery(self):
assert sensor.state == 'problem'
assert sensor.state_attributes['problem'] == 'battery low'

def test_initial_states(self):
"""Test plant initialises attributes if sensor already exists."""
self.hass.states.set(MOISTURE_ENTITY, 5,
{ATTR_UNIT_OF_MEASUREMENT: 'us/cm'})
plant_name = 'some_plant'
assert setup_component(self.hass, plant.DOMAIN, {
plant.DOMAIN: {
plant_name: GOOD_CONFIG
}
})
self.hass.block_till_done()
state = self.hass.states.get('plant.'+plant_name)
assert 5 == state.attributes[plant.READING_MOISTURE]

def test_update_states(self):
"""Test updating the state of a sensor.

Expand Down