Migrate IPMA to has entity name (#98572)

* Migrate IPMA to has entity name

* Migrate IPMA to has entity name
This commit is contained in:
Joost Lekkerkerker 2023-08-17 16:02:22 +02:00 committed by GitHub
parent ea5272ba62
commit 6f4294dc62
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 13 deletions

View File

@ -1,6 +1,9 @@
"""Base Entity for IPMA.""" """Base Entity for IPMA."""
from __future__ import annotations from __future__ import annotations
from pyipma.api import IPMA_API
from pyipma.location import Location
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
from homeassistant.helpers.entity import Entity from homeassistant.helpers.entity import Entity
@ -10,17 +13,20 @@ from .const import DOMAIN
class IPMADevice(Entity): class IPMADevice(Entity):
"""Common IPMA Device Information.""" """Common IPMA Device Information."""
def __init__(self, location) -> None: _attr_has_entity_name = True
def __init__(self, api: IPMA_API, location: Location) -> None:
"""Initialize device information.""" """Initialize device information."""
self._api = api
self._location = location self._location = location
self._attr_device_info = DeviceInfo( self._attr_device_info = DeviceInfo(
entry_type=DeviceEntryType.SERVICE, entry_type=DeviceEntryType.SERVICE,
identifiers={ identifiers={
( (
DOMAIN, DOMAIN,
f"{self._location.station_latitude}, {self._location.station_longitude}", f"{location.station_latitude}, {location.station_longitude}",
) )
}, },
manufacturer=DOMAIN, manufacturer=DOMAIN,
name=self._location.name, name=location.name,
) )

View File

@ -75,9 +75,8 @@ class IPMASensor(SensorEntity, IPMADevice):
description: IPMASensorEntityDescription, description: IPMASensorEntityDescription,
) -> None: ) -> None:
"""Initialize the IPMA Sensor.""" """Initialize the IPMA Sensor."""
IPMADevice.__init__(self, location) IPMADevice.__init__(self, api, location)
self.entity_description = description self.entity_description = description
self._api = api
self._attr_unique_id = f"{self._location.station_latitude}, {self._location.station_longitude}, {self.entity_description.key}" self._attr_unique_id = f"{self._location.station_latitude}, {self._location.station_longitude}, {self.entity_description.key}"
@Throttle(MIN_TIME_BETWEEN_UPDATES) @Throttle(MIN_TIME_BETWEEN_UPDATES)

View File

@ -25,7 +25,6 @@ from homeassistant.components.weather import (
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ( from homeassistant.const import (
CONF_MODE, CONF_MODE,
CONF_NAME,
UnitOfPressure, UnitOfPressure,
UnitOfSpeed, UnitOfSpeed,
UnitOfTemperature, UnitOfTemperature,
@ -56,13 +55,14 @@ async def async_setup_entry(
"""Add a weather entity from a config_entry.""" """Add a weather entity from a config_entry."""
api = hass.data[DOMAIN][config_entry.entry_id][DATA_API] api = hass.data[DOMAIN][config_entry.entry_id][DATA_API]
location = hass.data[DOMAIN][config_entry.entry_id][DATA_LOCATION] location = hass.data[DOMAIN][config_entry.entry_id][DATA_LOCATION]
async_add_entities([IPMAWeather(location, api, config_entry.data)], True) async_add_entities([IPMAWeather(api, location, config_entry)], True)
class IPMAWeather(WeatherEntity, IPMADevice): class IPMAWeather(WeatherEntity, IPMADevice):
"""Representation of a weather condition.""" """Representation of a weather condition."""
_attr_attribution = ATTRIBUTION _attr_attribution = ATTRIBUTION
_attr_name = None
_attr_native_pressure_unit = UnitOfPressure.HPA _attr_native_pressure_unit = UnitOfPressure.HPA
_attr_native_temperature_unit = UnitOfTemperature.CELSIUS _attr_native_temperature_unit = UnitOfTemperature.CELSIUS
_attr_native_wind_speed_unit = UnitOfSpeed.KILOMETERS_PER_HOUR _attr_native_wind_speed_unit = UnitOfSpeed.KILOMETERS_PER_HOUR
@ -70,13 +70,13 @@ class IPMAWeather(WeatherEntity, IPMADevice):
WeatherEntityFeature.FORECAST_DAILY | WeatherEntityFeature.FORECAST_HOURLY WeatherEntityFeature.FORECAST_DAILY | WeatherEntityFeature.FORECAST_HOURLY
) )
def __init__(self, location: Location, api: IPMA_API, config) -> None: def __init__(
self, api: IPMA_API, location: Location, config_entry: ConfigEntry
) -> None:
"""Initialise the platform with a data instance and station name.""" """Initialise the platform with a data instance and station name."""
IPMADevice.__init__(self, location) IPMADevice.__init__(self, api, location)
self._api = api self._mode = config_entry.data.get(CONF_MODE)
self._attr_name = config.get(CONF_NAME, location.name) self._period = 1 if config_entry.data.get(CONF_MODE) == "hourly" else 24
self._mode = config.get(CONF_MODE)
self._period = 1 if config.get(CONF_MODE) == "hourly" else 24
self._observation = None self._observation = None
self._daily_forecast: list[IPMAForecast] | None = None self._daily_forecast: list[IPMAForecast] | None = None
self._hourly_forecast: list[IPMAForecast] | None = None self._hourly_forecast: list[IPMAForecast] | None = None