1 """Demo platform for the geolocation component."""
3 from __future__
import annotations
5 from datetime
import timedelta
7 from math
import cos, pi, radians, sin
17 _LOGGER = logging.getLogger(__name__)
19 AVG_KM_PER_DEGREE = 111.0
22 NUMBER_OF_DEMO_DEVICES = 5
48 add_entities: AddEntitiesCallback,
49 discovery_info: DiscoveryInfoType |
None =
None,
51 """Set up the Demo geolocations."""
56 """Device manager for demo geolocation events."""
58 def __init__(self, hass: HomeAssistant, add_entities: AddEntitiesCallback) ->
None:
59 """Initialise the demo geolocation event manager."""
62 self._managed_devices: list[DemoGeolocationEvent] = []
63 self.
_update_update(count=NUMBER_OF_DEMO_DEVICES)
67 """Generate a random event in vicinity of this HA instance."""
68 home_latitude = self.
_hass_hass.config.latitude
69 home_longitude = self.
_hass_hass.config.longitude
72 radius_in_degrees = random.random() * MAX_RADIUS_IN_KM / AVG_KM_PER_DEGREE
73 radius_in_km = radius_in_degrees * AVG_KM_PER_DEGREE
74 angle = random.random() * 2 * pi
77 latitude = home_latitude + radius_in_degrees * sin(angle)
78 longitude = home_longitude + radius_in_degrees * cos(angle) / cos(
79 radians(home_latitude)
82 event_name = random.choice(EVENT_NAMES)
84 event_name, radius_in_km, latitude, longitude, UnitOfLength.KILOMETERS
88 """Schedule regular updates based on configured time interval."""
91 lambda now: self.
_update_update(),
92 DEFAULT_UPDATE_INTERVAL,
93 cancel_on_shutdown=
True,
96 def _update(self, count: int = 1) ->
None:
97 """Remove events and add new random events."""
99 for _
in range(1, count + 1):
100 if self._managed_devices:
101 device = random.choice(self._managed_devices)
103 _LOGGER.debug(
"Removing %s", device)
104 self._managed_devices.
remove(device)
105 self.
_hass_hass.add_job(device.async_remove())
108 for _
in range(1, count + 1):
110 _LOGGER.debug(
"Adding %s", new_device)
111 new_devices.append(new_device)
112 self._managed_devices.append(new_device)
117 """Represents a demo geolocation event."""
119 _attr_should_poll =
False
127 unit_of_measurement: str,
129 """Initialize entity with data provided."""
138 """Return source value of this external event."""
143 """Return distance value of this external event."""
148 """Return latitude value of this external event."""
153 """Return longitude value of this external event."""
158 """Return the unit of measurement."""
float|None latitude(self)
str unit_of_measurement(self)
float|None distance(self)
float|None longitude(self)
None __init__(self, str name, float distance, float latitude, float longitude, str unit_of_measurement)
None __init__(self, HomeAssistant hass, AddEntitiesCallback add_entities)
DemoGeolocationEvent _generate_random_event(self)
None _update(self, int count=1)
None _init_regular_updates(self)
bool remove(self, _T matcher)
None setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback add_entities, DiscoveryInfoType|None discovery_info=None)