-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathweather.py
75 lines (61 loc) · 2.5 KB
/
weather.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from homeassistant.components.weather import WeatherEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import TEMP_CELSIUS
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, CoordinatorEntity
from .const import (API_CONDITION, API_CURRENT, API_FORECAST, API_HUMIDITY,
API_TEMPERATURE, ATTRIBUTION, CONF_LOCATION, DOMAIN, MANUFACTURER)
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry, async_add_entities: AddEntitiesCallback) -> None:
"""Set up HKO weather entity based on a config entry."""
name = config_entry.data[CONF_LOCATION]
unique_id = config_entry.unique_id
coordinator = hass.data[DOMAIN][config_entry.entry_id]
async_add_entities([HKOEntity(name, unique_id, coordinator)], False)
class HKOEntity(CoordinatorEntity, WeatherEntity):
"""Define a HKO entity."""
def __init__(self, name, unique_id, coordinator: DataUpdateCoordinator) -> None:
"""Initialise the weather platform."""
super().__init__(coordinator)
self._name = name
self._unique_id = unique_id
@property
def name(self) -> str:
"""Return the name."""
return self._name
@property
def attribution(self) -> str:
"""Return the attribution."""
return ATTRIBUTION
@property
def unique_id(self) -> str:
"""Return a unique_id for this entity."""
return self._unique_id
@property
def device_info(self):
"""Return the device info."""
return {
"identifiers": {(DOMAIN, self._unique_id)},
"manufacturer": MANUFACTURER,
"entry_type": "service",
}
@property
def condition(self) -> str:
"""Return the current condition."""
return self.coordinator.data[API_FORECAST][0][API_CONDITION]
@property
def temperature(self) -> int:
"""Return the temperature."""
return self.coordinator.data[API_CURRENT][API_TEMPERATURE]
@property
def temperature_unit(self) -> str:
"""Return the unit of measurement."""
return TEMP_CELSIUS
@property
def humidity(self) -> int:
"""Return the humidity."""
return self.coordinator.data[API_CURRENT][API_HUMIDITY]
@property
def forecast(self) -> list:
"""Return the forecast array."""
return self.coordinator.data[API_FORECAST]