Home Assistant Unofficial Reference 2024.12.1
weather.py
Go to the documentation of this file.
1 """Support for the HKO service."""
2 
4  Forecast,
5  WeatherEntity,
6  WeatherEntityFeature,
7 )
8 from homeassistant.config_entries import ConfigEntry
9 from homeassistant.const import UnitOfTemperature
10 from homeassistant.core import HomeAssistant
11 from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
12 from homeassistant.helpers.entity_platform import AddEntitiesCallback
13 from homeassistant.helpers.update_coordinator import CoordinatorEntity
14 
15 from .const import (
16  API_CONDITION,
17  API_CURRENT,
18  API_FORECAST,
19  API_HUMIDITY,
20  API_TEMPERATURE,
21  ATTRIBUTION,
22  DOMAIN,
23  MANUFACTURER,
24 )
25 from .coordinator import HKOUpdateCoordinator
26 
27 
29  hass: HomeAssistant,
30  config_entry: ConfigEntry,
31  async_add_entities: AddEntitiesCallback,
32 ) -> None:
33  """Add a HKO weather entity from a config_entry."""
34  assert config_entry.unique_id is not None
35  unique_id = config_entry.unique_id
36  coordinator = hass.data[DOMAIN][config_entry.entry_id]
37  async_add_entities([HKOEntity(unique_id, coordinator)], False)
38 
39 
40 class HKOEntity(CoordinatorEntity[HKOUpdateCoordinator], WeatherEntity):
41  """Define a HKO entity."""
42 
43  _attr_has_entity_name = True
44  _attr_name = None
45  _attr_native_temperature_unit = UnitOfTemperature.CELSIUS
46  _attr_supported_features = WeatherEntityFeature.FORECAST_DAILY
47  _attr_attribution = ATTRIBUTION
48 
49  def __init__(self, unique_id: str, coordinator: HKOUpdateCoordinator) -> None:
50  """Initialise the weather platform."""
51  super().__init__(coordinator)
52  self._attr_unique_id_attr_unique_id = unique_id
53  self._attr_device_info_attr_device_info = DeviceInfo(
54  identifiers={(DOMAIN, unique_id)},
55  manufacturer=MANUFACTURER,
56  entry_type=DeviceEntryType.SERVICE,
57  )
58 
59  @property
60  def condition(self) -> str:
61  """Return the current condition."""
62  return self.coordinator.data[API_FORECAST][0][API_CONDITION]
63 
64  @property
65  def native_temperature(self) -> int:
66  """Return the temperature."""
67  return self.coordinator.data[API_CURRENT][API_TEMPERATURE]
68 
69  @property
70  def humidity(self) -> int:
71  """Return the humidity."""
72  return self.coordinator.data[API_CURRENT][API_HUMIDITY]
73 
74  async def async_forecast_daily(self) -> list[Forecast] | None:
75  """Return the forecast data."""
76  return self.coordinator.data[API_FORECAST]
list[Forecast]|None async_forecast_daily(self)
Definition: weather.py:74
None __init__(self, str unique_id, HKOUpdateCoordinator coordinator)
Definition: weather.py:49
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: weather.py:32