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,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)