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."""
from __future__ import annotations
from pyspcwebgw import SpcWebGateway
from pyspcwebgw.area import Area
from pyspcwebgw.const import AreaMode
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
def _get_alarm_state(area):
def _get_alarm_state(area: Area) -> str | None:
"""Get the alarm state."""
if area.verified_alarm:
@ -44,20 +46,21 @@ async def async_setup_platform(
"""Set up the SPC alarm control panel platform."""
if discovery_info is None:
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()])
class SpcAlarm(alarm.AlarmControlPanelEntity):
"""Representation of the SPC alarm panel."""
_attr_should_poll = False
_attr_supported_features = (
AlarmControlPanelEntityFeature.ARM_HOME
| AlarmControlPanelEntityFeature.ARM_AWAY
| AlarmControlPanelEntityFeature.ARM_NIGHT
)
def __init__(self, area, api):
def __init__(self, area: Area, api: SpcWebGateway) -> None:
"""Initialize the SPC alarm panel."""
self._area = area
self._api = api
@ -73,27 +76,22 @@ class SpcAlarm(alarm.AlarmControlPanelEntity):
)
@callback
def _update_callback(self):
def _update_callback(self) -> None:
"""Call update method."""
self.async_schedule_update_ha_state(True)
@property
def should_poll(self):
"""No polling needed."""
return False
@property
def name(self):
def name(self) -> str:
"""Return the name of the device."""
return self._area.name
@property
def changed_by(self):
def changed_by(self) -> str:
"""Return the user the last change was triggered by."""
return self._area.last_changed_by
@property
def state(self):
def state(self) -> str | None:
"""Return the state of the device."""
return _get_alarm_state(self._area)

View File

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