Update home zone when core config updated (#24237)

* Update home zone when core config updated

* Lint
pull/24465/head
Paulus Schoutsen 2019-05-31 23:03:45 -07:00
parent d88d57f3bb
commit 6ea0575a4a
7 changed files with 57 additions and 31 deletions

View File

@ -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(

View File

@ -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,13 +92,25 @@ 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:
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

View File

@ -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:

View File

@ -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(

View File

@ -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

View File

@ -444,7 +444,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

View File

@ -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')