Shorthanded attrs for met integration (#100334)

pull/100337/head
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
1 changed files with 26 additions and 48 deletions

View File

@ -2,7 +2,7 @@
from __future__ import annotations
from types import MappingProxyType
from typing import Any
from typing import TYPE_CHECKING, Any
from homeassistant.components.weather import (
ATTR_FORECAST_CONDITION,
@ -51,11 +51,16 @@ async def async_setup_entry(
coordinator: MetDataUpdateCoordinator = hass.data[DOMAIN][config_entry.entry_id]
entity_registry = er.async_get(hass)
entities = [
MetWeather(
coordinator, config_entry.data, hass.config.units is METRIC_SYSTEM, False
)
]
name: str | None
is_metric = hass.config.units is METRIC_SYSTEM
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
if entity_registry.async_get_entity_id(
@ -63,10 +68,9 @@ async def async_setup_entry(
DOMAIN,
_calculate_unique_id(config_entry.data, True),
):
name = f"{name} hourly"
entities.append(
MetWeather(
coordinator, config_entry.data, hass.config.units is METRIC_SYSTEM, True
)
MetWeather(coordinator, config_entry.data, True, name, is_metric)
)
async_add_entities(entities)
@ -111,8 +115,9 @@ class MetWeather(SingleCoordinatorWeatherEntity[MetDataUpdateCoordinator]):
self,
coordinator: MetDataUpdateCoordinator,
config: MappingProxyType[str, Any],
is_metric: bool,
hourly: bool,
name: str,
is_metric: bool,
) -> None:
"""Initialise the platform with a data instance and site."""
super().__init__(coordinator)
@ -120,32 +125,17 @@ class MetWeather(SingleCoordinatorWeatherEntity[MetDataUpdateCoordinator]):
self._config = config
self._is_metric = is_metric
self._hourly = hourly
@property
def track_home(self) -> Any | bool:
"""Return if we are tracking home."""
return self._config.get(CONF_TRACK_HOME, False)
@property
def name(self) -> str:
"""Return the name of the sensor."""
name = self._config.get(CONF_NAME)
name_appendix = ""
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
self._attr_entity_registry_enabled_default = not hourly
self._attr_device_info = 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",
)
self._attr_track_home = self._config.get(CONF_TRACK_HOME, False)
self._attr_name = name
@property
def condition(self) -> str | None:
@ -248,15 +238,3 @@ class MetWeather(SingleCoordinatorWeatherEntity[MetDataUpdateCoordinator]):
def _async_forecast_hourly(self) -> list[Forecast] | None:
"""Return the hourly forecast in native units."""
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",
)