Update home zone when core config updated (#24237)
* Update home zone when core config updated * Lintpull/24465/head
parent
d88d57f3bb
commit
6ea0575a4a
|
@ -56,7 +56,7 @@ async def websocket_update_config(hass, connection, msg):
|
||||||
data.pop('type')
|
data.pop('type')
|
||||||
|
|
||||||
try:
|
try:
|
||||||
await hass.config.update(**data)
|
await hass.config.async_update(**data)
|
||||||
connection.send_result(msg['id'])
|
connection.send_result(msg['id'])
|
||||||
except ValueError as err:
|
except ValueError as err:
|
||||||
connection.send_error(
|
connection.send_error(
|
||||||
|
|
|
@ -3,10 +3,12 @@ import logging
|
||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
|
from homeassistant.core import callback
|
||||||
from homeassistant.loader import bind_hass
|
from homeassistant.loader import bind_hass
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.const import (
|
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 import config_per_platform
|
||||||
from homeassistant.helpers.entity import async_generate_entity_id
|
from homeassistant.helpers.entity import async_generate_entity_id
|
||||||
from homeassistant.util import slugify
|
from homeassistant.util import slugify
|
||||||
|
@ -90,13 +92,25 @@ async def async_setup(hass, config):
|
||||||
hass.async_create_task(zone.async_update_ha_state())
|
hass.async_create_task(zone.async_update_ha_state())
|
||||||
entities.add(zone.entity_id)
|
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,
|
zone = Zone(hass, hass.config.location_name,
|
||||||
hass.config.latitude, hass.config.longitude,
|
hass.config.latitude, hass.config.longitude,
|
||||||
DEFAULT_RADIUS, ICON_HOME, False)
|
DEFAULT_RADIUS, ICON_HOME, False)
|
||||||
zone.entity_id = ENTITY_ID_HOME
|
zone.entity_id = ENTITY_ID_HOME
|
||||||
hass.async_create_task(zone.async_update_ha_state())
|
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
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -23,21 +23,18 @@ def in_zone(zone, latitude, longitude, radius=0) -> bool:
|
||||||
class Zone(Entity):
|
class Zone(Entity):
|
||||||
"""Representation of a Zone."""
|
"""Representation of a Zone."""
|
||||||
|
|
||||||
|
name = None
|
||||||
|
|
||||||
def __init__(self, hass, name, latitude, longitude, radius, icon, passive):
|
def __init__(self, hass, name, latitude, longitude, radius, icon, passive):
|
||||||
"""Initialize the zone."""
|
"""Initialize the zone."""
|
||||||
self.hass = hass
|
self.hass = hass
|
||||||
self._name = name
|
self.name = name
|
||||||
self._latitude = latitude
|
self.latitude = latitude
|
||||||
self._longitude = longitude
|
self.longitude = longitude
|
||||||
self._radius = radius
|
self._radius = radius
|
||||||
self._icon = icon
|
self._icon = icon
|
||||||
self._passive = passive
|
self._passive = passive
|
||||||
|
|
||||||
@property
|
|
||||||
def name(self):
|
|
||||||
"""Return the name of the zone."""
|
|
||||||
return self._name
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def state(self):
|
def state(self):
|
||||||
"""Return the state property really does nothing for a zone."""
|
"""Return the state property really does nothing for a zone."""
|
||||||
|
@ -53,8 +50,8 @@ class Zone(Entity):
|
||||||
"""Return the state attributes of the zone."""
|
"""Return the state attributes of the zone."""
|
||||||
data = {
|
data = {
|
||||||
ATTR_HIDDEN: True,
|
ATTR_HIDDEN: True,
|
||||||
ATTR_LATITUDE: self._latitude,
|
ATTR_LATITUDE: self.latitude,
|
||||||
ATTR_LONGITUDE: self._longitude,
|
ATTR_LONGITUDE: self.longitude,
|
||||||
ATTR_RADIUS: self._radius,
|
ATTR_RADIUS: self._radius,
|
||||||
}
|
}
|
||||||
if self._passive:
|
if self._passive:
|
||||||
|
|
|
@ -1288,10 +1288,7 @@ class Config:
|
||||||
unit_system: Optional[str] = None,
|
unit_system: Optional[str] = None,
|
||||||
location_name: Optional[str] = None,
|
location_name: Optional[str] = None,
|
||||||
time_zone: Optional[str] = None) -> None:
|
time_zone: Optional[str] = None) -> None:
|
||||||
"""Update the configuration from a dictionary.
|
"""Update the configuration from a dictionary."""
|
||||||
|
|
||||||
Async friendly.
|
|
||||||
"""
|
|
||||||
self.config_source = source
|
self.config_source = source
|
||||||
if latitude is not None:
|
if latitude is not None:
|
||||||
self.latitude = latitude
|
self.latitude = latitude
|
||||||
|
@ -1309,11 +1306,8 @@ class Config:
|
||||||
if time_zone is not None:
|
if time_zone is not None:
|
||||||
self.set_time_zone(time_zone)
|
self.set_time_zone(time_zone)
|
||||||
|
|
||||||
async def update(self, **kwargs: Any) -> None:
|
async def async_update(self, **kwargs: Any) -> None:
|
||||||
"""Update the configuration from a dictionary.
|
"""Update the configuration from a dictionary."""
|
||||||
|
|
||||||
Async friendly.
|
|
||||||
"""
|
|
||||||
self._update(source=SOURCE_STORAGE, **kwargs)
|
self._update(source=SOURCE_STORAGE, **kwargs)
|
||||||
await self.async_store()
|
await self.async_store()
|
||||||
self.hass.bus.async_fire(
|
self.hass.bus.async_fire(
|
||||||
|
|
|
@ -221,3 +221,24 @@ class TestComponentZone(unittest.TestCase):
|
||||||
|
|
||||||
assert zone.zone.in_zone(self.hass.states.get('zone.passive_zone'),
|
assert zone.zone.in_zone(self.hass.states.get('zone.passive_zone'),
|
||||||
latitude, longitude)
|
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
|
||||||
|
|
|
@ -444,7 +444,7 @@ async def test_updating_configuration(hass, hass_storage):
|
||||||
hass_storage["core.config"] = dict(core_data)
|
hass_storage["core.config"] = dict(core_data)
|
||||||
await config_util.async_process_ha_core_config(
|
await config_util.async_process_ha_core_config(
|
||||||
hass, {'whitelist_external_dirs': '/tmp'})
|
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 = copy.deepcopy(core_data)
|
||||||
new_core_data['data']['latitude'] = 50
|
new_core_data['data']['latitude'] = 50
|
||||||
|
|
|
@ -955,7 +955,7 @@ async def test_event_on_update(hass, hass_storage):
|
||||||
|
|
||||||
assert hass.config.latitude != 12
|
assert hass.config.latitude != 12
|
||||||
|
|
||||||
await hass.config.update(latitude=12)
|
await hass.config.async_update(latitude=12)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
assert hass.config.latitude == 12
|
assert hass.config.latitude == 12
|
||||||
|
@ -963,10 +963,10 @@ async def test_event_on_update(hass, hass_storage):
|
||||||
assert events[0].data == {'latitude': 12}
|
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."""
|
"""Test bad timezone raises ValueError."""
|
||||||
with pytest.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')
|
@patch('homeassistant.core.monotonic')
|
||||||
|
|
Loading…
Reference in New Issue