Shorthanded attrs for met integration (#100334)

This commit is contained in:
Jan Bouwhuis 2023-09-14 00:11:47 +02:00 committed by GitHub
parent a7c6abfed1
commit 01410c9fbb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,7 +2,7 @@
from __future__ import annotations from __future__ import annotations
from types import MappingProxyType from types import MappingProxyType
from typing import Any from typing import TYPE_CHECKING, Any
from homeassistant.components.weather import ( from homeassistant.components.weather import (
ATTR_FORECAST_CONDITION, ATTR_FORECAST_CONDITION,
@ -51,11 +51,16 @@ async def async_setup_entry(
coordinator: MetDataUpdateCoordinator = hass.data[DOMAIN][config_entry.entry_id] coordinator: MetDataUpdateCoordinator = hass.data[DOMAIN][config_entry.entry_id]
entity_registry = er.async_get(hass) entity_registry = er.async_get(hass)
entities = [ name: str | None
MetWeather( is_metric = hass.config.units is METRIC_SYSTEM
coordinator, config_entry.data, hass.config.units is METRIC_SYSTEM, False if config_entry.data.get(CONF_TRACK_HOME, False):
) name = hass.config.location_name
] elif (name := config_entry.data.get(CONF_NAME)) and name is None:
name = DEFAULT_NAME
elif TYPE_CHECKING:
assert isinstance(name, str)
entities = [MetWeather(coordinator, config_entry.data, False, name, is_metric)]
# Add hourly entity to legacy config entries # Add hourly entity to legacy config entries
if entity_registry.async_get_entity_id( if entity_registry.async_get_entity_id(
@ -63,10 +68,9 @@ async def async_setup_entry(
DOMAIN, DOMAIN,
_calculate_unique_id(config_entry.data, True), _calculate_unique_id(config_entry.data, True),
): ):
name = f"{name} hourly"
entities.append( entities.append(
MetWeather( MetWeather(coordinator, config_entry.data, True, name, is_metric)
coordinator, config_entry.data, hass.config.units is METRIC_SYSTEM, True
)
) )
async_add_entities(entities) async_add_entities(entities)
@ -111,8 +115,9 @@ class MetWeather(SingleCoordinatorWeatherEntity[MetDataUpdateCoordinator]):
self, self,
coordinator: MetDataUpdateCoordinator, coordinator: MetDataUpdateCoordinator,
config: MappingProxyType[str, Any], config: MappingProxyType[str, Any],
is_metric: bool,
hourly: bool, hourly: bool,
name: str,
is_metric: bool,
) -> None: ) -> None:
"""Initialise the platform with a data instance and site.""" """Initialise the platform with a data instance and site."""
super().__init__(coordinator) super().__init__(coordinator)
@ -120,32 +125,17 @@ class MetWeather(SingleCoordinatorWeatherEntity[MetDataUpdateCoordinator]):
self._config = config self._config = config
self._is_metric = is_metric self._is_metric = is_metric
self._hourly = hourly self._hourly = hourly
self._attr_entity_registry_enabled_default = not hourly
@property self._attr_device_info = DeviceInfo(
def track_home(self) -> Any | bool: name="Forecast",
"""Return if we are tracking home.""" entry_type=DeviceEntryType.SERVICE,
return self._config.get(CONF_TRACK_HOME, False) identifiers={(DOMAIN,)}, # type: ignore[arg-type]
manufacturer="Met.no",
@property model="Forecast",
def name(self) -> str: configuration_url="https://www.met.no/en",
"""Return the name of the sensor.""" )
name = self._config.get(CONF_NAME) self._attr_track_home = self._config.get(CONF_TRACK_HOME, False)
name_appendix = "" self._attr_name = name
if self._hourly:
name_appendix = " hourly"
if name is not None:
return f"{name}{name_appendix}"
if self.track_home:
return f"{self.hass.config.location_name}{name_appendix}"
return f"{DEFAULT_NAME}{name_appendix}"
@property
def entity_registry_enabled_default(self) -> bool:
"""Return if the entity should be enabled when first added to the entity registry."""
return not self._hourly
@property @property
def condition(self) -> str | None: def condition(self) -> str | None:
@ -248,15 +238,3 @@ class MetWeather(SingleCoordinatorWeatherEntity[MetDataUpdateCoordinator]):
def _async_forecast_hourly(self) -> list[Forecast] | None: def _async_forecast_hourly(self) -> list[Forecast] | None:
"""Return the hourly forecast in native units.""" """Return the hourly forecast in native units."""
return self._forecast(True) return self._forecast(True)
@property
def device_info(self) -> DeviceInfo:
"""Device info."""
return DeviceInfo(
name="Forecast",
entry_type=DeviceEntryType.SERVICE,
identifiers={(DOMAIN,)}, # type: ignore[arg-type]
manufacturer="Met.no",
model="Forecast",
configuration_url="https://www.met.no/en",
)