From 0bf652ca960358ba151118469686a1bf0221b331 Mon Sep 17 00:00:00 2001 From: Malte Franken Date: Mon, 20 Mar 2023 23:26:38 +1100 Subject: [PATCH] Refactor constants in geo_json_events integration (#89912) move constants to separate file --- .../components/geo_json_events/const.py | 15 +++++++++++++ .../geo_json_events/geo_location.py | 22 +++++++++---------- .../components/geo_json_events/manager.py | 6 ++--- .../geo_json_events/test_geo_location.py | 18 +++++++-------- 4 files changed, 38 insertions(+), 23 deletions(-) create mode 100644 homeassistant/components/geo_json_events/const.py diff --git a/homeassistant/components/geo_json_events/const.py b/homeassistant/components/geo_json_events/const.py new file mode 100644 index 00000000000..4c73be3995e --- /dev/null +++ b/homeassistant/components/geo_json_events/const.py @@ -0,0 +1,15 @@ +"""Define constants for the GeoJSON events integration.""" +from __future__ import annotations + +from datetime import timedelta +from typing import Final + +DOMAIN: Final = "geo_json_events" + +ATTR_EXTERNAL_ID: Final = "external_id" +DEFAULT_RADIUS_IN_KM: Final = 20.0 +DEFAULT_SCAN_INTERVAL: Final = timedelta(minutes=5) +SOURCE: Final = "geo_json_events" + +SIGNAL_DELETE_ENTITY: Final = "geo_json_events_delete_{}" +SIGNAL_UPDATE_ENTITY: Final = "geo_json_events_update_{}" diff --git a/homeassistant/components/geo_json_events/geo_location.py b/homeassistant/components/geo_json_events/geo_location.py index 2df049dd9cd..df2978b654e 100644 --- a/homeassistant/components/geo_json_events/geo_location.py +++ b/homeassistant/components/geo_json_events/geo_location.py @@ -26,18 +26,18 @@ from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType +from .const import ( + ATTR_EXTERNAL_ID, + DEFAULT_RADIUS_IN_KM, + DEFAULT_SCAN_INTERVAL, + SIGNAL_DELETE_ENTITY, + SIGNAL_UPDATE_ENTITY, + SOURCE, +) from .manager import GeoJsonFeedEntityManager _LOGGER = logging.getLogger(__name__) -ATTR_EXTERNAL_ID = "external_id" - -DEFAULT_RADIUS_IN_KM = 20.0 - -SCAN_INTERVAL = timedelta(minutes=5) - -SOURCE = "geo_json_events" - PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( { vol.Required(CONF_URL): cv.string, @@ -56,7 +56,7 @@ async def async_setup_platform( ) -> None: """Set up the GeoJSON Events platform.""" url: str = config[CONF_URL] - scan_interval: timedelta = config.get(CONF_SCAN_INTERVAL, SCAN_INTERVAL) + scan_interval: timedelta = config.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL) coordinates: tuple[float, float] = ( config.get(CONF_LATITUDE, hass.config.latitude), config.get(CONF_LONGITUDE, hass.config.longitude), @@ -106,12 +106,12 @@ class GeoJsonLocationEvent(GeolocationEvent): """Call when entity is added to hass.""" self._remove_signal_delete = async_dispatcher_connect( self.hass, - f"geo_json_events_delete_{self._external_id}", + SIGNAL_DELETE_ENTITY.format(self._external_id), self._delete_callback, ) self._remove_signal_update = async_dispatcher_connect( self.hass, - f"geo_json_events_update_{self._external_id}", + SIGNAL_UPDATE_ENTITY.format(self._external_id), self._update_callback, ) diff --git a/homeassistant/components/geo_json_events/manager.py b/homeassistant/components/geo_json_events/manager.py index 6c51e6dd723..a999d224ac7 100644 --- a/homeassistant/components/geo_json_events/manager.py +++ b/homeassistant/components/geo_json_events/manager.py @@ -12,7 +12,7 @@ from homeassistant.helpers import aiohttp_client from homeassistant.helpers.dispatcher import async_dispatcher_send from homeassistant.helpers.event import async_track_time_interval -DOMAIN = "geo_json_events" +from .const import DOMAIN, SIGNAL_DELETE_ENTITY, SIGNAL_UPDATE_ENTITY _LOGGER = logging.getLogger(__name__) @@ -77,8 +77,8 @@ class GeoJsonFeedEntityManager: async def _update_entity(self, external_id: str) -> None: """Update entity.""" - async_dispatcher_send(self._hass, f"geo_json_events_update_{external_id}") + async_dispatcher_send(self._hass, SIGNAL_UPDATE_ENTITY.format(external_id)) async def _remove_entity(self, external_id: str) -> None: """Remove entity.""" - async_dispatcher_send(self._hass, f"geo_json_events_delete_{external_id}") + async_dispatcher_send(self._hass, SIGNAL_DELETE_ENTITY.format(external_id)) diff --git a/tests/components/geo_json_events/test_geo_location.py b/tests/components/geo_json_events/test_geo_location.py index b79c5f15046..529d78fd83c 100644 --- a/tests/components/geo_json_events/test_geo_location.py +++ b/tests/components/geo_json_events/test_geo_location.py @@ -5,9 +5,9 @@ from aio_geojson_generic_client import GenericFeed from freezegun import freeze_time from homeassistant.components import geo_location -from homeassistant.components.geo_json_events.geo_location import ( +from homeassistant.components.geo_json_events.const import ( ATTR_EXTERNAL_ID, - SCAN_INTERVAL, + DEFAULT_SCAN_INTERVAL, ) from homeassistant.components.geo_location import ATTR_SOURCE from homeassistant.const import ( @@ -132,7 +132,7 @@ async def test_setup(hass: HomeAssistant) -> None: "OK", [mock_entry_1, mock_entry_4, mock_entry_3], ) - async_fire_time_changed(hass, utcnow + SCAN_INTERVAL) + async_fire_time_changed(hass, utcnow + DEFAULT_SCAN_INTERVAL) await hass.async_block_till_done() all_states = hass.states.async_all() @@ -141,7 +141,7 @@ async def test_setup(hass: HomeAssistant) -> None: # Simulate an update - empty data, but successful update, # so no changes to entities. mock_feed_update.return_value = "OK_NO_DATA", None - async_fire_time_changed(hass, utcnow + 2 * SCAN_INTERVAL) + async_fire_time_changed(hass, utcnow + 2 * DEFAULT_SCAN_INTERVAL) await hass.async_block_till_done() all_states = hass.states.async_all() @@ -149,7 +149,7 @@ async def test_setup(hass: HomeAssistant) -> None: # Simulate an update - empty data, removes all entities mock_feed_update.return_value = "ERROR", None - async_fire_time_changed(hass, utcnow + 3 * SCAN_INTERVAL) + async_fire_time_changed(hass, utcnow + 3 * DEFAULT_SCAN_INTERVAL) await hass.async_block_till_done() all_states = hass.states.async_all() @@ -227,7 +227,7 @@ async def test_setup_race_condition(hass: HomeAssistant) -> None: # Simulate an update - empty data, removes all entities mock_feed_update.return_value = "ERROR", None - async_fire_time_changed(hass, utcnow + SCAN_INTERVAL) + async_fire_time_changed(hass, utcnow + DEFAULT_SCAN_INTERVAL) await hass.async_block_till_done() all_states = hass.states.async_all() @@ -237,7 +237,7 @@ async def test_setup_race_condition(hass: HomeAssistant) -> None: # Simulate an update - 1 entry mock_feed_update.return_value = "OK", [mock_entry_1] - async_fire_time_changed(hass, utcnow + 2 * SCAN_INTERVAL) + async_fire_time_changed(hass, utcnow + 2 * DEFAULT_SCAN_INTERVAL) await hass.async_block_till_done() all_states = hass.states.async_all() @@ -247,7 +247,7 @@ async def test_setup_race_condition(hass: HomeAssistant) -> None: # Simulate an update - 1 entry mock_feed_update.return_value = "OK", [mock_entry_1] - async_fire_time_changed(hass, utcnow + 3 * SCAN_INTERVAL) + async_fire_time_changed(hass, utcnow + 3 * DEFAULT_SCAN_INTERVAL) await hass.async_block_till_done() all_states = hass.states.async_all() @@ -257,7 +257,7 @@ async def test_setup_race_condition(hass: HomeAssistant) -> None: # Simulate an update - empty data, removes all entities mock_feed_update.return_value = "ERROR", None - async_fire_time_changed(hass, utcnow + 4 * SCAN_INTERVAL) + async_fire_time_changed(hass, utcnow + 4 * DEFAULT_SCAN_INTERVAL) await hass.async_block_till_done() all_states = hass.states.async_all()