From b90636f64041045521b56b63c792ade6cec4cd80 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Fri, 31 May 2019 23:03:45 -0700 Subject: [PATCH] Update home zone when core config updated (#24237) * Update home zone when core config updated * Lint --- homeassistant/components/config/core.py | 2 +- homeassistant/components/zone/__init__.py | 28 +++++++++++++++++------ homeassistant/components/zone/zone.py | 17 ++++++-------- homeassistant/core.py | 12 +++------- tests/components/zone/test_init.py | 21 +++++++++++++++++ tests/test_config.py | 2 +- tests/test_core.py | 6 ++--- 7 files changed, 57 insertions(+), 31 deletions(-) diff --git a/homeassistant/components/config/core.py b/homeassistant/components/config/core.py index 31abb832f23..a83516bdc37 100644 --- a/homeassistant/components/config/core.py +++ b/homeassistant/components/config/core.py @@ -56,7 +56,7 @@ async def websocket_update_config(hass, connection, msg): data.pop('type') try: - await hass.config.update(**data) + await hass.config.async_update(**data) connection.send_result(msg['id']) except ValueError as err: connection.send_error( diff --git a/homeassistant/components/zone/__init__.py b/homeassistant/components/zone/__init__.py index 0340964561c..1ece0dbaaa1 100644 --- a/homeassistant/components/zone/__init__.py +++ b/homeassistant/components/zone/__init__.py @@ -3,10 +3,12 @@ import logging import voluptuous as vol +from homeassistant.core import callback from homeassistant.loader import bind_hass import homeassistant.helpers.config_validation as cv from homeassistant.const import ( - CONF_NAME, CONF_LATITUDE, CONF_LONGITUDE, CONF_ICON, CONF_RADIUS) + CONF_NAME, CONF_LATITUDE, CONF_LONGITUDE, CONF_ICON, CONF_RADIUS, + EVENT_CORE_CONFIG_UPDATE) from homeassistant.helpers import config_per_platform from homeassistant.helpers.entity import async_generate_entity_id from homeassistant.util import slugify @@ -90,12 +92,24 @@ async def async_setup(hass, config): hass.async_create_task(zone.async_update_ha_state()) entities.add(zone.entity_id) - if ENTITY_ID_HOME not in entities and HOME_ZONE not in zone_entries: - zone = Zone(hass, hass.config.location_name, - hass.config.latitude, hass.config.longitude, - DEFAULT_RADIUS, ICON_HOME, False) - zone.entity_id = ENTITY_ID_HOME - hass.async_create_task(zone.async_update_ha_state()) + if ENTITY_ID_HOME in entities or HOME_ZONE in zone_entries: + return True + + zone = Zone(hass, hass.config.location_name, + hass.config.latitude, hass.config.longitude, + DEFAULT_RADIUS, ICON_HOME, False) + zone.entity_id = ENTITY_ID_HOME + hass.async_create_task(zone.async_update_ha_state()) + + @callback + def core_config_updated(_): + """Handle core config updated.""" + zone.name = hass.config.location_name + zone.latitude = hass.config.latitude + zone.longitude = hass.config.longitude + zone.async_write_ha_state() + + hass.bus.async_listen(EVENT_CORE_CONFIG_UPDATE, core_config_updated) return True diff --git a/homeassistant/components/zone/zone.py b/homeassistant/components/zone/zone.py index 20155e06311..51e2a623def 100644 --- a/homeassistant/components/zone/zone.py +++ b/homeassistant/components/zone/zone.py @@ -23,21 +23,18 @@ def in_zone(zone, latitude, longitude, radius=0) -> bool: class Zone(Entity): """Representation of a Zone.""" + name = None + def __init__(self, hass, name, latitude, longitude, radius, icon, passive): """Initialize the zone.""" self.hass = hass - self._name = name - self._latitude = latitude - self._longitude = longitude + self.name = name + self.latitude = latitude + self.longitude = longitude self._radius = radius self._icon = icon self._passive = passive - @property - def name(self): - """Return the name of the zone.""" - return self._name - @property def state(self): """Return the state property really does nothing for a zone.""" @@ -53,8 +50,8 @@ class Zone(Entity): """Return the state attributes of the zone.""" data = { ATTR_HIDDEN: True, - ATTR_LATITUDE: self._latitude, - ATTR_LONGITUDE: self._longitude, + ATTR_LATITUDE: self.latitude, + ATTR_LONGITUDE: self.longitude, ATTR_RADIUS: self._radius, } if self._passive: diff --git a/homeassistant/core.py b/homeassistant/core.py index b732eb0d4b3..ef15a4b11a0 100644 --- a/homeassistant/core.py +++ b/homeassistant/core.py @@ -1288,10 +1288,7 @@ class Config: unit_system: Optional[str] = None, location_name: Optional[str] = None, time_zone: Optional[str] = None) -> None: - """Update the configuration from a dictionary. - - Async friendly. - """ + """Update the configuration from a dictionary.""" self.config_source = source if latitude is not None: self.latitude = latitude @@ -1309,11 +1306,8 @@ class Config: if time_zone is not None: self.set_time_zone(time_zone) - async def update(self, **kwargs: Any) -> None: - """Update the configuration from a dictionary. - - Async friendly. - """ + async def async_update(self, **kwargs: Any) -> None: + """Update the configuration from a dictionary.""" self._update(source=SOURCE_STORAGE, **kwargs) await self.async_store() self.hass.bus.async_fire( diff --git a/tests/components/zone/test_init.py b/tests/components/zone/test_init.py index 576be0ce03c..11fe9ae5e66 100644 --- a/tests/components/zone/test_init.py +++ b/tests/components/zone/test_init.py @@ -221,3 +221,24 @@ class TestComponentZone(unittest.TestCase): assert zone.zone.in_zone(self.hass.states.get('zone.passive_zone'), latitude, longitude) + + +async def test_core_config_update(hass): + """Test updating core config will update home zone.""" + assert await setup.async_setup_component(hass, 'zone', {}) + + home = hass.states.get('zone.home') + + await hass.config.async_update( + location_name='Updated Name', + latitude=10, + longitude=20, + ) + await hass.async_block_till_done() + + home_updated = hass.states.get('zone.home') + + assert home is not home_updated + assert home_updated.name == 'Updated Name' + assert home_updated.attributes['latitude'] == 10 + assert home_updated.attributes['longitude'] == 20 diff --git a/tests/test_config.py b/tests/test_config.py index 5579679937b..a42fc3b809c 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -421,7 +421,7 @@ async def test_updating_configuration(hass, hass_storage): hass_storage["core.config"] = dict(core_data) await config_util.async_process_ha_core_config( hass, {'whitelist_external_dirs': '/tmp'}) - await hass.config.update(latitude=50) + await hass.config.async_update(latitude=50) new_core_data = copy.deepcopy(core_data) new_core_data['data']['latitude'] = 50 diff --git a/tests/test_core.py b/tests/test_core.py index 15ab2baf3a9..00bd4265da7 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -955,7 +955,7 @@ async def test_event_on_update(hass, hass_storage): assert hass.config.latitude != 12 - await hass.config.update(latitude=12) + await hass.config.async_update(latitude=12) await hass.async_block_till_done() assert hass.config.latitude == 12 @@ -963,10 +963,10 @@ async def test_event_on_update(hass, hass_storage): assert events[0].data == {'latitude': 12} -def test_bad_timezone_raises_value_error(hass): +async def test_bad_timezone_raises_value_error(hass): """Test bad timezone raises ValueError.""" with pytest.raises(ValueError): - hass.config.set_time_zone('not_a_timezone') + await hass.config.async_update(time_zone='not_a_timezone') @patch('homeassistant.core.monotonic')