Refactor constants in geo_json_events integration (#89912)

move constants to separate file
This commit is contained in:
Malte Franken 2023-03-20 23:26:38 +11:00 committed by GitHub
parent 9f1e170851
commit 0bf652ca96
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 38 additions and 23 deletions

View file

@ -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_{}"

View file

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

View file

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

View file

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