Convert skybell to use NamedTuple (#53269)

* Convert to NamedTuple.

* Second version.

* Use names instead of index.

* Review comments.

* Add meta variable.

* Review comment.

* Review comments.
pull/53284/head
jan iversen 2021-07-21 19:42:30 +02:00 committed by GitHub
parent a1df3519db
commit aed7cb9120
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 24 deletions

View File

@ -1,5 +1,8 @@
"""Binary sensor support for the Skybell HD Doorbell."""
from __future__ import annotations
from datetime import timedelta
from typing import NamedTuple
import voluptuous as vol
@ -16,10 +19,26 @@ from . import DEFAULT_ENTITY_NAMESPACE, DOMAIN as SKYBELL_DOMAIN, SkybellDevice
SCAN_INTERVAL = timedelta(seconds=10)
# Sensor types: Name, device_class, event
class SkybellBinarySensorMetadata(NamedTuple):
"""Metadata for an individual Skybell binary_sensor."""
name: str
device_class: str
event: str
SENSOR_TYPES = {
"button": ["Button", DEVICE_CLASS_OCCUPANCY, "device:sensor:button"],
"motion": ["Motion", DEVICE_CLASS_MOTION, "device:sensor:motion"],
"button": SkybellBinarySensorMetadata(
"Button",
device_class=DEVICE_CLASS_OCCUPANCY,
event="device:sensor:button",
),
"motion": SkybellBinarySensorMetadata(
"Motion",
device_class=DEVICE_CLASS_MOTION,
event="device:sensor:motion",
),
}
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
@ -53,18 +72,12 @@ class SkybellBinarySensor(SkybellDevice, BinarySensorEntity):
"""Initialize a binary sensor for a Skybell device."""
super().__init__(device)
self._sensor_type = sensor_type
self._name = "{} {}".format(
self._device.name, SENSOR_TYPES[self._sensor_type][0]
)
self._device_class = SENSOR_TYPES[self._sensor_type][1]
self._metadata = SENSOR_TYPES[self._sensor_type]
self._attr_name = f"{self._device.name} {self._metadata.name}"
self._device_class = self._metadata.device_class
self._event = {}
self._state = None
@property
def name(self):
"""Return the name of the sensor."""
return self._name
@property
def is_on(self):
"""Return True if the binary sensor is on."""
@ -88,7 +101,7 @@ class SkybellBinarySensor(SkybellDevice, BinarySensorEntity):
"""Get the latest data and updates the state."""
super().update()
event = self._device.latest(SENSOR_TYPES[self._sensor_type][2])
event = self._device.latest(self._metadata.event)
self._state = bool(event and event.get("id") != self._event.get("id"))

View File

@ -1,4 +1,8 @@
"""Switch support for the Skybell HD Doorbell."""
from __future__ import annotations
from typing import NamedTuple
import voluptuous as vol
from homeassistant.components.switch import PLATFORM_SCHEMA, SwitchEntity
@ -7,10 +11,20 @@ import homeassistant.helpers.config_validation as cv
from . import DEFAULT_ENTITY_NAMESPACE, DOMAIN as SKYBELL_DOMAIN, SkybellDevice
# Switch types: Name
class SkybellSwitchMetadata(NamedTuple):
"""Metadata for an individual Skybell switch."""
name: str
SWITCH_TYPES = {
"do_not_disturb": ["Do Not Disturb"],
"motion_sensor": ["Motion Sensor"],
"do_not_disturb": SkybellSwitchMetadata(
"Do Not Disturb",
),
"motion_sensor": SkybellSwitchMetadata(
"Motion Sensor",
),
}
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
@ -44,14 +58,8 @@ class SkybellSwitch(SkybellDevice, SwitchEntity):
"""Initialize a light for a Skybell device."""
super().__init__(device)
self._switch_type = switch_type
self._name = "{} {}".format(
self._device.name, SWITCH_TYPES[self._switch_type][0]
)
@property
def name(self):
"""Return the name of the sensor."""
return self._name
metadata = SWITCH_TYPES[self._switch_type]
self._attr_name = f"{self._device.name} {metadata.name}"
def turn_on(self, **kwargs):
"""Turn on the switch."""