-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselect.py
132 lines (108 loc) · 4.15 KB
/
select.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
"""Support for the Netatmo climate schedule selector."""
from __future__ import annotations
import logging
from homeassistant.components.select import SelectEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import (
CONF_URL_ENERGY,
DATA_SCHEDULES,
DOMAIN,
EVENT_TYPE_SCHEDULE,
MANUFACTURER,
NETATMO_CREATE_SELECT,
)
from .data_handler import HOME, SIGNAL_NAME, NetatmoHome
from .entity import NetatmoBaseEntity
_LOGGER = logging.getLogger(__name__)
async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
"""Set up the Netatmo energy platform schedule selector."""
@callback
def _create_entity(netatmo_home: NetatmoHome) -> None:
entity = NetatmoScheduleSelect(netatmo_home)
async_add_entities([entity])
entry.async_on_unload(
async_dispatcher_connect(hass, NETATMO_CREATE_SELECT, _create_entity)
)
class NetatmoScheduleSelect(NetatmoBaseEntity, SelectEntity):
"""Representation a Netatmo thermostat schedule selector."""
_attr_name = None
def __init__(self, netatmo_home: NetatmoHome) -> None:
"""Initialize the select entity."""
super().__init__(netatmo_home.data_handler)
self.home = netatmo_home.home
self._publishers.extend(
[
{
"name": HOME,
"home_id": self.home.entity_id,
SIGNAL_NAME: netatmo_home.signal_name,
},
]
)
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, self.home.entity_id)},
name=self.home.name,
manufacturer=MANUFACTURER,
model="Climate",
configuration_url=CONF_URL_ENERGY,
)
self._attr_unique_id = f"{self.home.entity_id}-schedule-select"
self._attr_current_option = getattr(self.home.get_selected_schedule(), "name")
self._attr_options = [
schedule.name for schedule in self.home.schedules.values() if schedule.name
]
async def async_added_to_hass(self) -> None:
"""Entity created."""
await super().async_added_to_hass()
self.async_on_remove(
async_dispatcher_connect(
self.hass,
f"signal-{DOMAIN}-webhook-{EVENT_TYPE_SCHEDULE}",
self.handle_event,
)
)
@callback
def handle_event(self, event: dict) -> None:
"""Handle webhook events."""
data = event["data"]
if self.home.entity_id != data["home_id"]:
return
if data["event_type"] == EVENT_TYPE_SCHEDULE and "schedule_id" in data:
self._attr_current_option = getattr(
self.hass.data[DOMAIN][DATA_SCHEDULES][self.home.entity_id].get(
data["schedule_id"]
),
"name",
)
self.async_write_ha_state()
async def async_select_option(self, option: str) -> None:
"""Change the selected option."""
for sid, schedule in self.hass.data[DOMAIN][DATA_SCHEDULES][
self.home.entity_id
].items():
if schedule.name != option:
continue
_LOGGER.debug(
"Setting %s schedule to %s (%s)",
self.home.entity_id,
option,
sid,
)
await self.home.async_switch_schedule(schedule_id=sid)
break
@callback
def async_update_callback(self) -> None:
"""Update the entity's state."""
self._attr_current_option = getattr(self.home.get_selected_schedule(), "name")
self.hass.data[DOMAIN][DATA_SCHEDULES][self.home.entity_id] = (
self.home.schedules
)
self._attr_options = [
schedule.name for schedule in self.home.schedules.values() if schedule.name
]