2019-02-13 21:21:14 +01:00
|
|
|
"""Support for Envisalink sensors (shows panel info)."""
|
2022-01-16 22:34:05 +01:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2016-06-19 12:45:07 -05:00
|
|
|
import logging
|
2017-02-23 22:02:56 +01:00
|
|
|
|
2021-03-22 12:52:29 +01:00
|
|
|
from homeassistant.components.sensor import SensorEntity
|
2022-01-16 22:34:05 +01:00
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2017-02-23 22:02:56 +01:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2022-01-16 22:34:05 +01:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
2016-06-19 12:45:07 -05:00
|
|
|
|
2019-03-20 22:56:46 -07:00
|
|
|
from . import (
|
2019-07-31 12:25:30 -07:00
|
|
|
CONF_PARTITIONNAME,
|
|
|
|
DATA_EVL,
|
|
|
|
PARTITION_SCHEMA,
|
|
|
|
SIGNAL_KEYPAD_UPDATE,
|
|
|
|
SIGNAL_PARTITION_UPDATE,
|
|
|
|
EnvisalinkDevice,
|
|
|
|
)
|
2019-03-20 22:56:46 -07:00
|
|
|
|
2016-06-19 12:45:07 -05:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2022-01-16 22:34:05 +01:00
|
|
|
async def async_setup_platform(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
discovery_info: DiscoveryInfoType | None = None,
|
|
|
|
) -> None:
|
2022-01-25 19:21:58 -08:00
|
|
|
"""Perform the setup for Envisalink sensor entities."""
|
2022-01-16 22:34:05 +01:00
|
|
|
if not discovery_info:
|
|
|
|
return
|
2019-07-31 12:25:30 -07:00
|
|
|
configured_partitions = discovery_info["partitions"]
|
2017-02-23 22:02:56 +01:00
|
|
|
|
2022-01-25 19:21:58 -08:00
|
|
|
entities = []
|
2017-02-23 22:02:56 +01:00
|
|
|
for part_num in configured_partitions:
|
2022-01-25 19:21:58 -08:00
|
|
|
entity_config_data = PARTITION_SCHEMA(configured_partitions[part_num])
|
|
|
|
entity = EnvisalinkSensor(
|
2019-07-31 12:25:30 -07:00
|
|
|
hass,
|
2022-01-25 19:21:58 -08:00
|
|
|
entity_config_data[CONF_PARTITIONNAME],
|
2019-07-31 12:25:30 -07:00
|
|
|
part_num,
|
|
|
|
hass.data[DATA_EVL].alarm_state["partition"][part_num],
|
|
|
|
hass.data[DATA_EVL],
|
|
|
|
)
|
2019-02-13 21:21:14 +01:00
|
|
|
|
2022-01-25 19:21:58 -08:00
|
|
|
entities.append(entity)
|
2017-02-23 22:02:56 +01:00
|
|
|
|
2022-01-25 19:21:58 -08:00
|
|
|
async_add_entities(entities)
|
2016-06-19 12:45:07 -05:00
|
|
|
|
|
|
|
|
2021-03-22 20:05:13 +01:00
|
|
|
class EnvisalinkSensor(EnvisalinkDevice, SensorEntity):
|
2016-07-01 21:39:30 +02:00
|
|
|
"""Representation of an Envisalink keypad."""
|
2016-06-19 12:45:07 -05:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
def __init__(self, hass, partition_name, partition_number, info, controller):
|
2016-06-19 12:45:07 -05:00
|
|
|
"""Initialize the sensor."""
|
2019-07-31 12:25:30 -07:00
|
|
|
self._icon = "mdi:alarm"
|
2016-06-19 12:45:07 -05:00
|
|
|
self._partition_number = partition_number
|
2017-02-23 22:02:56 +01:00
|
|
|
|
2017-05-02 18:18:47 +02:00
|
|
|
_LOGGER.debug("Setting up sensor for partition: %s", partition_name)
|
2020-04-05 16:01:41 +02:00
|
|
|
super().__init__(f"{partition_name} Keypad", info, controller)
|
2017-02-23 22:02:56 +01:00
|
|
|
|
2022-08-20 07:52:55 +02:00
|
|
|
async def async_added_to_hass(self) -> None:
|
2017-03-02 10:20:57 +01:00
|
|
|
"""Register callbacks."""
|
2022-01-25 19:21:58 -08:00
|
|
|
self.async_on_remove(
|
|
|
|
async_dispatcher_connect(
|
|
|
|
self.hass, SIGNAL_KEYPAD_UPDATE, self.async_update_callback
|
|
|
|
)
|
|
|
|
)
|
|
|
|
self.async_on_remove(
|
|
|
|
async_dispatcher_connect(
|
|
|
|
self.hass, SIGNAL_PARTITION_UPDATE, self.async_update_callback
|
|
|
|
)
|
2019-07-31 12:25:30 -07:00
|
|
|
)
|
2016-06-19 12:45:07 -05:00
|
|
|
|
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Return the icon if any."""
|
|
|
|
return self._icon
|
|
|
|
|
|
|
|
@property
|
2021-08-11 18:57:12 +02:00
|
|
|
def native_value(self):
|
2016-06-19 12:45:07 -05:00
|
|
|
"""Return the overall state."""
|
2019-07-31 12:25:30 -07:00
|
|
|
return self._info["status"]["alpha"]
|
2016-06-19 12:45:07 -05:00
|
|
|
|
|
|
|
@property
|
2021-03-11 16:51:03 +01:00
|
|
|
def extra_state_attributes(self):
|
2016-06-19 12:45:07 -05:00
|
|
|
"""Return the state attributes."""
|
2019-07-31 12:25:30 -07:00
|
|
|
return self._info["status"]
|
2016-06-19 12:45:07 -05:00
|
|
|
|
2017-02-23 22:02:56 +01:00
|
|
|
@callback
|
2022-01-25 19:21:58 -08:00
|
|
|
def async_update_callback(self, partition):
|
2016-06-19 12:45:07 -05:00
|
|
|
"""Update the partition state in HA, if needed."""
|
|
|
|
if partition is None or int(partition) == self._partition_number:
|
2020-04-01 14:19:51 -07:00
|
|
|
self.async_write_ha_state()
|