Use attributes in spc alarm and binary sensor (#74120)

This commit is contained in:
epenet 2022-06-28 14:49:01 +02:00 committed by GitHub
parent c4ff317ec6
commit b75a6d265d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 26 deletions

View File

@ -1,6 +1,8 @@
"""Support for Vanderbilt (formerly Siemens) SPC alarm systems.""" """Support for Vanderbilt (formerly Siemens) SPC alarm systems."""
from __future__ import annotations from __future__ import annotations
from pyspcwebgw import SpcWebGateway
from pyspcwebgw.area import Area
from pyspcwebgw.const import AreaMode from pyspcwebgw.const import AreaMode
import homeassistant.components.alarm_control_panel as alarm import homeassistant.components.alarm_control_panel as alarm
@ -20,7 +22,7 @@ from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from . import DATA_API, SIGNAL_UPDATE_ALARM from . import DATA_API, SIGNAL_UPDATE_ALARM
def _get_alarm_state(area): def _get_alarm_state(area: Area) -> str | None:
"""Get the alarm state.""" """Get the alarm state."""
if area.verified_alarm: if area.verified_alarm:
@ -44,20 +46,21 @@ async def async_setup_platform(
"""Set up the SPC alarm control panel platform.""" """Set up the SPC alarm control panel platform."""
if discovery_info is None: if discovery_info is None:
return return
api = hass.data[DATA_API] api: SpcWebGateway = hass.data[DATA_API]
async_add_entities([SpcAlarm(area=area, api=api) for area in api.areas.values()]) async_add_entities([SpcAlarm(area=area, api=api) for area in api.areas.values()])
class SpcAlarm(alarm.AlarmControlPanelEntity): class SpcAlarm(alarm.AlarmControlPanelEntity):
"""Representation of the SPC alarm panel.""" """Representation of the SPC alarm panel."""
_attr_should_poll = False
_attr_supported_features = ( _attr_supported_features = (
AlarmControlPanelEntityFeature.ARM_HOME AlarmControlPanelEntityFeature.ARM_HOME
| AlarmControlPanelEntityFeature.ARM_AWAY | AlarmControlPanelEntityFeature.ARM_AWAY
| AlarmControlPanelEntityFeature.ARM_NIGHT | AlarmControlPanelEntityFeature.ARM_NIGHT
) )
def __init__(self, area, api): def __init__(self, area: Area, api: SpcWebGateway) -> None:
"""Initialize the SPC alarm panel.""" """Initialize the SPC alarm panel."""
self._area = area self._area = area
self._api = api self._api = api
@ -73,27 +76,22 @@ class SpcAlarm(alarm.AlarmControlPanelEntity):
) )
@callback @callback
def _update_callback(self): def _update_callback(self) -> None:
"""Call update method.""" """Call update method."""
self.async_schedule_update_ha_state(True) self.async_schedule_update_ha_state(True)
@property @property
def should_poll(self): def name(self) -> str:
"""No polling needed."""
return False
@property
def name(self):
"""Return the name of the device.""" """Return the name of the device."""
return self._area.name return self._area.name
@property @property
def changed_by(self): def changed_by(self) -> str:
"""Return the user the last change was triggered by.""" """Return the user the last change was triggered by."""
return self._area.last_changed_by return self._area.last_changed_by
@property @property
def state(self): def state(self) -> str | None:
"""Return the state of the device.""" """Return the state of the device."""
return _get_alarm_state(self._area) return _get_alarm_state(self._area)

View File

@ -1,7 +1,9 @@
"""Support for Vanderbilt (formerly Siemens) SPC alarm systems.""" """Support for Vanderbilt (formerly Siemens) SPC alarm systems."""
from __future__ import annotations from __future__ import annotations
from pyspcwebgw import SpcWebGateway
from pyspcwebgw.const import ZoneInput, ZoneType from pyspcwebgw.const import ZoneInput, ZoneType
from pyspcwebgw.zone import Zone
from homeassistant.components.binary_sensor import ( from homeassistant.components.binary_sensor import (
BinarySensorDeviceClass, BinarySensorDeviceClass,
@ -15,12 +17,12 @@ from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from . import DATA_API, SIGNAL_UPDATE_SENSOR from . import DATA_API, SIGNAL_UPDATE_SENSOR
def _get_device_class(zone_type): def _get_device_class(zone_type: ZoneType) -> BinarySensorDeviceClass | None:
return { return {
ZoneType.ALARM: BinarySensorDeviceClass.MOTION, ZoneType.ALARM: BinarySensorDeviceClass.MOTION,
ZoneType.ENTRY_EXIT: BinarySensorDeviceClass.OPENING, ZoneType.ENTRY_EXIT: BinarySensorDeviceClass.OPENING,
ZoneType.FIRE: BinarySensorDeviceClass.SMOKE, ZoneType.FIRE: BinarySensorDeviceClass.SMOKE,
ZoneType.TECHNICAL: "power", ZoneType.TECHNICAL: BinarySensorDeviceClass.POWER,
}.get(zone_type) }.get(zone_type)
@ -33,7 +35,7 @@ async def async_setup_platform(
"""Set up the SPC binary sensor.""" """Set up the SPC binary sensor."""
if discovery_info is None: if discovery_info is None:
return return
api = hass.data[DATA_API] api: SpcWebGateway = hass.data[DATA_API]
async_add_entities( async_add_entities(
[ [
SpcBinarySensor(zone) SpcBinarySensor(zone)
@ -46,11 +48,13 @@ async def async_setup_platform(
class SpcBinarySensor(BinarySensorEntity): class SpcBinarySensor(BinarySensorEntity):
"""Representation of a sensor based on a SPC zone.""" """Representation of a sensor based on a SPC zone."""
def __init__(self, zone): _attr_should_poll = False
def __init__(self, zone: Zone) -> None:
"""Initialize the sensor device.""" """Initialize the sensor device."""
self._zone = zone self._zone = zone
async def async_added_to_hass(self): async def async_added_to_hass(self) -> None:
"""Call for adding new entities.""" """Call for adding new entities."""
self.async_on_remove( self.async_on_remove(
async_dispatcher_connect( async_dispatcher_connect(
@ -61,26 +65,21 @@ class SpcBinarySensor(BinarySensorEntity):
) )
@callback @callback
def _update_callback(self): def _update_callback(self) -> None:
"""Call update method.""" """Call update method."""
self.async_schedule_update_ha_state(True) self.async_schedule_update_ha_state(True)
@property @property
def name(self): def name(self) -> str:
"""Return the name of the device.""" """Return the name of the device."""
return self._zone.name return self._zone.name
@property @property
def is_on(self): def is_on(self) -> bool:
"""Whether the device is switched on.""" """Whether the device is switched on."""
return self._zone.input == ZoneInput.OPEN return self._zone.input == ZoneInput.OPEN
@property @property
def should_poll(self): def device_class(self) -> BinarySensorDeviceClass | None:
"""No polling needed."""
return False
@property
def device_class(self):
"""Return the device class.""" """Return the device class."""
return _get_device_class(self._zone.type) return _get_device_class(self._zone.type)