2021-07-12 22:27:48 +00:00
|
|
|
"""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.dispatcher import async_dispatcher_connect
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
|
|
|
|
from .const import (
|
2022-09-26 01:55:58 +00:00
|
|
|
CONF_URL_ENERGY,
|
2021-07-12 22:27:48 +00:00
|
|
|
DATA_SCHEDULES,
|
|
|
|
DOMAIN,
|
|
|
|
EVENT_TYPE_SCHEDULE,
|
2022-09-26 01:55:58 +00:00
|
|
|
NETATMO_CREATE_SELECT,
|
2021-12-02 09:31:54 +00:00
|
|
|
)
|
2022-09-26 01:55:58 +00:00
|
|
|
from .data_handler import HOME, SIGNAL_NAME, NetatmoHome
|
2021-07-12 22:27:48 +00:00
|
|
|
from .netatmo_entity_base import NetatmoBase
|
|
|
|
|
|
|
|
_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."""
|
|
|
|
|
2022-09-26 01:55:58 +00:00
|
|
|
@callback
|
|
|
|
def _create_entity(netatmo_home: NetatmoHome) -> None:
|
|
|
|
entity = NetatmoScheduleSelect(netatmo_home)
|
|
|
|
async_add_entities([entity])
|
2021-07-12 22:27:48 +00:00
|
|
|
|
2022-09-26 01:55:58 +00:00
|
|
|
entry.async_on_unload(
|
|
|
|
async_dispatcher_connect(hass, NETATMO_CREATE_SELECT, _create_entity)
|
|
|
|
)
|
2021-07-12 22:27:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
class NetatmoScheduleSelect(NetatmoBase, SelectEntity):
|
|
|
|
"""Representation a Netatmo thermostat schedule selector."""
|
|
|
|
|
|
|
|
def __init__(
|
2022-09-26 01:55:58 +00:00
|
|
|
self,
|
|
|
|
netatmo_home: NetatmoHome,
|
2021-07-12 22:27:48 +00:00
|
|
|
) -> None:
|
|
|
|
"""Initialize the select entity."""
|
|
|
|
SelectEntity.__init__(self)
|
2022-09-26 01:55:58 +00:00
|
|
|
super().__init__(netatmo_home.data_handler)
|
2021-12-02 09:31:54 +00:00
|
|
|
|
2022-09-26 01:55:58 +00:00
|
|
|
self._home = netatmo_home.home
|
|
|
|
self._home_id = self._home.entity_id
|
2021-12-02 09:31:54 +00:00
|
|
|
|
2022-09-26 01:55:58 +00:00
|
|
|
self._signal_name = netatmo_home.signal_name
|
2022-07-27 12:17:38 +00:00
|
|
|
self._publishers.extend(
|
2021-07-12 22:27:48 +00:00
|
|
|
[
|
|
|
|
{
|
2022-09-26 01:55:58 +00:00
|
|
|
"name": HOME,
|
|
|
|
"home_id": self._home.entity_id,
|
|
|
|
SIGNAL_NAME: self._signal_name,
|
2021-07-12 22:27:48 +00:00
|
|
|
},
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2021-12-02 09:31:54 +00:00
|
|
|
self._device_name = self._home.name
|
2022-09-26 01:55:58 +00:00
|
|
|
self._attr_name = f"{self._device_name}"
|
2021-07-12 22:27:48 +00:00
|
|
|
|
|
|
|
self._model: str = "NATherm1"
|
2022-09-26 01:55:58 +00:00
|
|
|
self._config_url = CONF_URL_ENERGY
|
2021-07-12 22:27:48 +00:00
|
|
|
|
|
|
|
self._attr_unique_id = f"{self._home_id}-schedule-select"
|
|
|
|
|
2021-12-02 09:31:54 +00:00
|
|
|
self._attr_current_option = getattr(self._home.get_selected_schedule(), "name")
|
2022-09-26 01:55:58 +00:00
|
|
|
self._attr_options = [
|
|
|
|
schedule.name for schedule in self._home.schedules.values()
|
|
|
|
]
|
2021-07-12 22:27:48 +00:00
|
|
|
|
|
|
|
async def async_added_to_hass(self) -> None:
|
|
|
|
"""Entity created."""
|
|
|
|
await super().async_added_to_hass()
|
|
|
|
|
2022-09-26 01:55:58 +00:00
|
|
|
self.async_on_remove(
|
|
|
|
async_dispatcher_connect(
|
|
|
|
self.hass,
|
|
|
|
f"signal-{DOMAIN}-webhook-{EVENT_TYPE_SCHEDULE}",
|
|
|
|
self.handle_event,
|
2021-07-12 22:27:48 +00:00
|
|
|
)
|
2022-09-26 01:55:58 +00:00
|
|
|
)
|
2021-07-12 22:27:48 +00:00
|
|
|
|
2021-07-30 18:07:23 +00:00
|
|
|
@callback
|
|
|
|
def handle_event(self, event: dict) -> None:
|
2021-07-12 22:27:48 +00:00
|
|
|
"""Handle webhook events."""
|
|
|
|
data = event["data"]
|
|
|
|
|
|
|
|
if self._home_id != data["home_id"]:
|
|
|
|
return
|
|
|
|
|
|
|
|
if data["event_type"] == EVENT_TYPE_SCHEDULE and "schedule_id" in data:
|
2021-12-02 09:31:54 +00:00
|
|
|
self._attr_current_option = getattr(
|
|
|
|
self.hass.data[DOMAIN][DATA_SCHEDULES][self._home_id].get(
|
|
|
|
data["schedule_id"]
|
|
|
|
),
|
|
|
|
"name",
|
|
|
|
)
|
2021-07-12 22:27:48 +00:00
|
|
|
self.async_write_ha_state()
|
|
|
|
|
|
|
|
async def async_select_option(self, option: str) -> None:
|
|
|
|
"""Change the selected option."""
|
2021-12-02 09:31:54 +00:00
|
|
|
for sid, schedule in self.hass.data[DOMAIN][DATA_SCHEDULES][
|
|
|
|
self._home_id
|
|
|
|
].items():
|
|
|
|
if schedule.name != option:
|
2021-07-12 22:27:48 +00:00
|
|
|
continue
|
|
|
|
_LOGGER.debug(
|
|
|
|
"Setting %s schedule to %s (%s)",
|
|
|
|
self._home_id,
|
|
|
|
option,
|
|
|
|
sid,
|
|
|
|
)
|
2022-09-26 01:55:58 +00:00
|
|
|
await self._home.async_switch_schedule(schedule_id=sid)
|
2021-07-12 22:27:48 +00:00
|
|
|
break
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def async_update_callback(self) -> None:
|
|
|
|
"""Update the entity's state."""
|
2021-12-02 09:31:54 +00:00
|
|
|
self._attr_current_option = getattr(self._home.get_selected_schedule(), "name")
|
|
|
|
self.hass.data[DOMAIN][DATA_SCHEDULES][self._home_id] = self._home.schedules
|
|
|
|
self._attr_options = [
|
2022-09-26 01:55:58 +00:00
|
|
|
schedule.name for schedule in self._home.schedules.values()
|
2021-12-02 09:31:54 +00:00
|
|
|
]
|