2020-03-20 02:03:51 +00:00
|
|
|
"""The nexia integration base entity."""
|
2022-01-16 17:51:16 +00:00
|
|
|
from nexia.thermostat import NexiaThermostat
|
|
|
|
from nexia.zone import NexiaThermostatZone
|
|
|
|
|
2020-03-23 16:01:48 +00:00
|
|
|
from homeassistant.const import ATTR_ATTRIBUTION
|
2022-01-16 17:51:16 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect, dispatcher_send
|
2021-10-24 09:34:45 +00:00
|
|
|
from homeassistant.helpers.entity import DeviceInfo
|
2020-08-30 12:57:02 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
2020-03-20 02:03:51 +00:00
|
|
|
|
2020-03-23 16:01:48 +00:00
|
|
|
from .const import (
|
|
|
|
ATTRIBUTION,
|
|
|
|
DOMAIN,
|
|
|
|
MANUFACTURER,
|
|
|
|
SIGNAL_THERMOSTAT_UPDATE,
|
|
|
|
SIGNAL_ZONE_UPDATE,
|
|
|
|
)
|
2021-10-24 09:34:45 +00:00
|
|
|
from .coordinator import NexiaDataUpdateCoordinator
|
2020-03-23 16:01:48 +00:00
|
|
|
|
2020-03-20 02:03:51 +00:00
|
|
|
|
2020-08-30 12:57:02 +00:00
|
|
|
class NexiaEntity(CoordinatorEntity):
|
2020-03-20 02:03:51 +00:00
|
|
|
"""Base class for nexia entities."""
|
|
|
|
|
2020-03-23 16:01:48 +00:00
|
|
|
def __init__(self, coordinator, name, unique_id):
|
2020-03-20 02:03:51 +00:00
|
|
|
"""Initialize the entity."""
|
2020-08-30 12:57:02 +00:00
|
|
|
super().__init__(coordinator)
|
2020-03-23 16:01:48 +00:00
|
|
|
self._unique_id = unique_id
|
|
|
|
self._name = name
|
2020-03-20 02:03:51 +00:00
|
|
|
|
2020-03-23 16:01:48 +00:00
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return the unique id."""
|
|
|
|
return self._unique_id
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
2021-03-11 19:11:25 +00:00
|
|
|
def extra_state_attributes(self):
|
2020-03-23 16:01:48 +00:00
|
|
|
"""Return the device specific state attributes."""
|
|
|
|
return {
|
|
|
|
ATTR_ATTRIBUTION: ATTRIBUTION,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class NexiaThermostatEntity(NexiaEntity):
|
|
|
|
"""Base class for nexia devices attached to a thermostat."""
|
|
|
|
|
|
|
|
def __init__(self, coordinator, thermostat, name, unique_id):
|
|
|
|
"""Initialize the entity."""
|
|
|
|
super().__init__(coordinator, name, unique_id)
|
2022-01-16 17:51:16 +00:00
|
|
|
self._thermostat: NexiaThermostat = thermostat
|
2020-03-23 16:01:48 +00:00
|
|
|
|
|
|
|
@property
|
2021-10-24 09:34:45 +00:00
|
|
|
def device_info(self) -> DeviceInfo:
|
2020-03-23 16:01:48 +00:00
|
|
|
"""Return the device_info of the device."""
|
2021-10-24 09:34:45 +00:00
|
|
|
assert isinstance(self.coordinator, NexiaDataUpdateCoordinator)
|
|
|
|
return DeviceInfo(
|
|
|
|
configuration_url=self.coordinator.nexia_home.root_url,
|
|
|
|
identifiers={(DOMAIN, self._thermostat.thermostat_id)},
|
|
|
|
manufacturer=MANUFACTURER,
|
|
|
|
model=self._thermostat.get_model(),
|
|
|
|
name=self._thermostat.get_name(),
|
|
|
|
sw_version=self._thermostat.get_firmware(),
|
|
|
|
)
|
2020-03-23 16:01:48 +00:00
|
|
|
|
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Listen for signals for services."""
|
|
|
|
await super().async_added_to_hass()
|
2020-04-20 18:39:33 +00:00
|
|
|
self.async_on_remove(
|
|
|
|
async_dispatcher_connect(
|
|
|
|
self.hass,
|
|
|
|
f"{SIGNAL_THERMOSTAT_UPDATE}-{self._thermostat.thermostat_id}",
|
|
|
|
self.async_write_ha_state,
|
|
|
|
)
|
2020-03-23 16:01:48 +00:00
|
|
|
)
|
|
|
|
|
2022-01-16 17:51:16 +00:00
|
|
|
def _signal_thermostat_update(self):
|
|
|
|
"""Signal a thermostat update.
|
|
|
|
|
|
|
|
Whenever the underlying library does an action against
|
|
|
|
a thermostat, the data for the thermostat and all
|
|
|
|
connected zone is updated.
|
|
|
|
|
|
|
|
Update all the zones on the thermostat.
|
|
|
|
"""
|
|
|
|
dispatcher_send(
|
|
|
|
self.hass, f"{SIGNAL_THERMOSTAT_UPDATE}-{self._thermostat.thermostat_id}"
|
|
|
|
)
|
|
|
|
|
2020-03-23 16:01:48 +00:00
|
|
|
|
|
|
|
class NexiaThermostatZoneEntity(NexiaThermostatEntity):
|
|
|
|
"""Base class for nexia devices attached to a thermostat."""
|
|
|
|
|
|
|
|
def __init__(self, coordinator, zone, name, unique_id):
|
|
|
|
"""Initialize the entity."""
|
|
|
|
super().__init__(coordinator, zone.thermostat, name, unique_id)
|
2022-01-16 17:51:16 +00:00
|
|
|
self._zone: NexiaThermostatZone = zone
|
2020-03-23 16:01:48 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def device_info(self):
|
|
|
|
"""Return the device_info of the device."""
|
|
|
|
data = super().device_info
|
2021-02-20 06:01:22 +00:00
|
|
|
zone_name = self._zone.get_name()
|
2020-03-23 16:01:48 +00:00
|
|
|
data.update(
|
|
|
|
{
|
|
|
|
"identifiers": {(DOMAIN, self._zone.zone_id)},
|
2021-02-20 06:01:22 +00:00
|
|
|
"name": zone_name,
|
|
|
|
"suggested_area": zone_name,
|
2020-03-23 16:01:48 +00:00
|
|
|
"via_device": (DOMAIN, self._zone.thermostat.thermostat_id),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
return data
|
|
|
|
|
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Listen for signals for services."""
|
|
|
|
await super().async_added_to_hass()
|
2020-04-20 18:39:33 +00:00
|
|
|
self.async_on_remove(
|
|
|
|
async_dispatcher_connect(
|
|
|
|
self.hass,
|
|
|
|
f"{SIGNAL_ZONE_UPDATE}-{self._zone.zone_id}",
|
|
|
|
self.async_write_ha_state,
|
|
|
|
)
|
2020-03-23 16:01:48 +00:00
|
|
|
)
|
2022-01-16 17:51:16 +00:00
|
|
|
|
|
|
|
def _signal_zone_update(self):
|
|
|
|
"""Signal a zone update.
|
|
|
|
|
|
|
|
Whenever the underlying library does an action against
|
|
|
|
a zone, the data for the zone is updated.
|
|
|
|
|
|
|
|
Update a single zone.
|
|
|
|
"""
|
|
|
|
dispatcher_send(self.hass, f"{SIGNAL_ZONE_UPDATE}-{self._zone.zone_id}")
|