core/homeassistant/components/envisalink/sensor.py

95 lines
2.8 KiB
Python
Raw Normal View History

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