Skip to content

Commit

Permalink
Add device info for Aemet (#120243)
Browse files Browse the repository at this point in the history
* Update sensor.py

* Update weather.py

* Update sensor.py

* ruff

* add device info to entity

* remove info from sensor

* remove info from weather

* ruff

* amend entity

* Update sensor.py

* Update weather.py

* ruff again

* add DOMAIN

* type unique_id

* Update entity.py

* Update entity.py

* assert

* update tests

* change snapshot
  • Loading branch information
luca-angemi authored Jun 24, 2024
1 parent fe3027f commit fdade67
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 4 deletions.
18 changes: 18 additions & 0 deletions homeassistant/components/aemet/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,32 @@
from aemet_opendata.helpers import dict_nested_value

from homeassistant.components.weather import Forecast
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
from homeassistant.helpers.update_coordinator import CoordinatorEntity

from .const import DOMAIN
from .coordinator import WeatherUpdateCoordinator


class AemetEntity(CoordinatorEntity[WeatherUpdateCoordinator]):
"""Define an AEMET entity."""

def __init__(
self,
coordinator: WeatherUpdateCoordinator,
name: str,
unique_id: str,
) -> None:
"""Initialize the entity."""
super().__init__(coordinator)
self._attr_device_info = DeviceInfo(
name=name,
entry_type=DeviceEntryType.SERVICE,
identifiers={(DOMAIN, unique_id)},
manufacturer="AEMET",
model="Forecast",
)

def get_aemet_forecast(self, forecast_mode: str) -> list[Forecast]:
"""Return AEMET entity forecast by mode."""
return self.coordinator.data["forecast"][forecast_mode]
Expand Down
6 changes: 4 additions & 2 deletions homeassistant/components/aemet/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,10 +392,12 @@ def __init__(
config_entry: ConfigEntry,
) -> None:
"""Initialize the sensor."""
super().__init__(coordinator)
assert config_entry.unique_id is not None
unique_id = config_entry.unique_id
super().__init__(coordinator, name, unique_id)
self.entity_description = description
self._attr_name = f"{name} {description.name}"
self._attr_unique_id = f"{config_entry.unique_id}-{description.key}"
self._attr_unique_id = f"{unique_id}-{description.key}"

@property
def native_value(self):
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/aemet/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def __init__(
coordinator: WeatherUpdateCoordinator,
) -> None:
"""Initialize the sensor."""
super().__init__(coordinator)
super().__init__(coordinator, name, unique_id)
self._attr_name = name
self._attr_unique_id = unique_id

Expand Down
2 changes: 1 addition & 1 deletion tests/components/aemet/snapshots/test_diagnostics.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
'pref_disable_polling': False,
'source': 'user',
'title': 'Mock Title',
'unique_id': None,
'unique_id': '**REDACTED**',
'version': 1,
}),
'coord_data': dict({
Expand Down
1 change: 1 addition & 0 deletions tests/components/aemet/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ async def async_init_integration(hass: HomeAssistant):
CONF_NAME: "AEMET",
},
entry_id="7442b231f139e813fc1939281123f220",
unique_id="40.30403754--3.72935236",
)
config_entry.add_to_hass(hass)

Expand Down

0 comments on commit fdade67

Please sign in to comment.