Skip to content

Commit

Permalink
geo_location "distance" attribute which is float (unlike the state wh…
Browse files Browse the repository at this point in the history
…ich is string)
  • Loading branch information
amitfin committed Oct 17, 2024
1 parent 5664fc8 commit 8d70daa
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
1 change: 1 addition & 0 deletions custom_components/oref_alert/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
ATTR_AREA: Final = "area"
ATTR_COUNTRY_ALERTS: Final = "country_alerts"
ATTR_COUNTRY_ACTIVE_ALERTS: Final = "country_active_alerts"
ATTR_DISTANCE: Final = "distance"
ATTR_SELECTED_AREAS_ALERTS: Final = "selected_areas_alerts"
ATTR_SELECTED_AREAS_ACTIVE_ALERTS: Final = "selected_areas_active_alerts"
ATTR_TIME_TO_SHELTER: Final = "time_to_shelter"
Expand Down
13 changes: 9 additions & 4 deletions custom_components/oref_alert/geo_location.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from homeassistant.util.location import vincenty

from .const import (
ATTR_DISTANCE,
DATA_COORDINATOR,
DOMAIN,
IST,
Expand Down Expand Up @@ -68,11 +69,15 @@ def __init__(
self._attr_latitude: float = AREA_INFO[area]["lat"]
self._attr_longitude: float = AREA_INFO[area]["long"]
self._attr_unit_of_measurement = UnitOfLength.KILOMETERS
self._attr_distance = vincenty(
(hass.config.latitude, hass.config.longitude),
(self._attr_latitude, self._attr_longitude),
self._attr_distance = round(
vincenty(
(hass.config.latitude, hass.config.longitude),
(self._attr_latitude, self._attr_longitude),
)
or 0,
1,
)
self._alert_attributes = attributes
self._alert_attributes = {**attributes, ATTR_DISTANCE: self._attr_distance}

@property
def suggested_object_id(self) -> str | None:
Expand Down
29 changes: 28 additions & 1 deletion tests/test_geo_location.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
UnitOfLength,
)
from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.template import Template, result_as_boolean
from pytest_homeassistant_custom_component.common import (
MockConfigEntry,
async_fire_time_changed,
)

from custom_components.oref_alert.const import (
ATTR_DISTANCE,
CONF_ALERT_ACTIVE_DURATION,
CONF_AREAS,
CONF_POLL_INTERVAL,
Expand Down Expand Up @@ -88,12 +90,13 @@ async def test_entity(
assert state.attributes[ATTR_LONGITUDE] == 34.4926
assert state.attributes[CONF_UNIT_OF_MEASUREMENT] == UnitOfLength.KILOMETERS
assert state.attributes[CONF_FRIENDLY_NAME] == "בארי"
assert state.attributes[ATTR_DISTANCE] == 80.6
assert state.attributes[ATTR_DATE] == dt_util.parse_datetime(
"2023-10-07 06:30:00+03:00"
)
assert state.attributes["category"] == 1
assert state.attributes["title"] == "ירי רקטות וטילים"
assert len(state.attributes) == 8
assert len(state.attributes) == 9
await async_shutdown(hass, config_id)


Expand Down Expand Up @@ -171,3 +174,27 @@ async def test_attributes_update(
assert state is not None
assert state.attributes["category"] == 2
await async_shutdown(hass, config_id)


async def test_distance_types(
hass: HomeAssistant,
aioclient_mock: AiohttpClientMocker,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test type of state and distance attribute."""
freezer.move_to("2023-10-07 06:30:00+03:00")
mock_urls(aioclient_mock, None, "single_alert_history.json")
config_id = await async_setup(hass)
state = hass.states.get(ENTITY_ID)
assert state is not None
assert result_as_boolean(
Template(f"{{{{ states.{ENTITY_ID}.state is string }}}}", hass).async_render(
parse_result=False
)
)
assert result_as_boolean(
Template(
f"{{{{ states.{ENTITY_ID}.attributes.{ATTR_DISTANCE} is float }}}}", hass
).async_render(parse_result=False)
)
await async_shutdown(hass, config_id)

0 comments on commit 8d70daa

Please sign in to comment.