Home Assistant Unofficial Reference 2024.12.1
weather.py
Go to the documentation of this file.
1 """Support for Meteoclimatic weather service."""
2 
3 from meteoclimatic import Condition
4 
5 from homeassistant.components.weather import WeatherEntity
6 from homeassistant.config_entries import ConfigEntry
7 from homeassistant.const import UnitOfPressure, UnitOfSpeed, UnitOfTemperature
8 from homeassistant.core import HomeAssistant
9 from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
10 from homeassistant.helpers.entity_platform import AddEntitiesCallback
12  CoordinatorEntity,
13  DataUpdateCoordinator,
14 )
15 
16 from .const import ATTRIBUTION, CONDITION_MAP, DOMAIN, MANUFACTURER, MODEL
17 
18 
19 def format_condition(condition):
20  """Return condition from dict CONDITION_MAP."""
21  if condition in CONDITION_MAP:
22  return CONDITION_MAP[condition]
23  if isinstance(condition, Condition):
24  return condition.value
25  return condition
26 
27 
29  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
30 ) -> None:
31  """Set up the Meteoclimatic weather platform."""
32  coordinator = hass.data[DOMAIN][entry.entry_id]
33 
34  async_add_entities([MeteoclimaticWeather(coordinator)], False)
35 
36 
38  """Representation of a weather condition."""
39 
40  _attr_attribution = ATTRIBUTION
41  _attr_native_pressure_unit = UnitOfPressure.HPA
42  _attr_native_temperature_unit = UnitOfTemperature.CELSIUS
43  _attr_native_wind_speed_unit = UnitOfSpeed.KILOMETERS_PER_HOUR
44 
45  def __init__(self, coordinator: DataUpdateCoordinator) -> None:
46  """Initialise the weather platform."""
47  super().__init__(coordinator)
48  self._unique_id_unique_id = self.coordinator.data["station"].code
49  self._name_name = self.coordinator.data["station"].name
50 
51  @property
52  def name(self):
53  """Return the name of the sensor."""
54  return self._name_name
55 
56  @property
57  def unique_id(self):
58  """Return the unique id of the sensor."""
59  return self._unique_id_unique_id
60 
61  @property
62  def device_info(self):
63  """Return the device info."""
64  return DeviceInfo(
65  entry_type=DeviceEntryType.SERVICE,
66  identifiers={(DOMAIN, self.platformplatform.config_entry.unique_id)},
67  manufacturer=MANUFACTURER,
68  model=MODEL,
69  name=self.coordinator.name,
70  )
71 
72  @property
73  def condition(self):
74  """Return the current condition."""
75  return format_condition(self.coordinator.data["weather"].condition)
76 
77  @property
78  def native_temperature(self):
79  """Return the temperature."""
80  return self.coordinator.data["weather"].temp_current
81 
82  @property
83  def humidity(self):
84  """Return the humidity."""
85  return self.coordinator.data["weather"].humidity_current
86 
87  @property
88  def native_pressure(self):
89  """Return the pressure."""
90  return self.coordinator.data["weather"].pressure_current
91 
92  @property
93  def native_wind_speed(self):
94  """Return the wind speed."""
95  return self.coordinator.data["weather"].wind_current
96 
97  @property
98  def wind_bearing(self):
99  """Return the wind bearing."""
100  return self.coordinator.data["weather"].wind_bearing
None __init__(self, DataUpdateCoordinator coordinator)
Definition: weather.py:45
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: weather.py:30