mirror of
https://github.com/home-assistant/core.git
synced 2025-07-26 06:37:52 +00:00
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.
This commit is contained in:
parent
a1df3519db
commit
aed7cb9120
@ -1,5 +1,8 @@
|
|||||||
"""Binary sensor support for the Skybell HD Doorbell."""
|
"""Binary sensor support for the Skybell HD Doorbell."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
|
from typing import NamedTuple
|
||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
@ -16,10 +19,26 @@ from . import DEFAULT_ENTITY_NAMESPACE, DOMAIN as SKYBELL_DOMAIN, SkybellDevice
|
|||||||
|
|
||||||
SCAN_INTERVAL = timedelta(seconds=10)
|
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 = {
|
SENSOR_TYPES = {
|
||||||
"button": ["Button", DEVICE_CLASS_OCCUPANCY, "device:sensor:button"],
|
"button": SkybellBinarySensorMetadata(
|
||||||
"motion": ["Motion", DEVICE_CLASS_MOTION, "device:sensor:motion"],
|
"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(
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
||||||
@ -53,18 +72,12 @@ class SkybellBinarySensor(SkybellDevice, BinarySensorEntity):
|
|||||||
"""Initialize a binary sensor for a Skybell device."""
|
"""Initialize a binary sensor for a Skybell device."""
|
||||||
super().__init__(device)
|
super().__init__(device)
|
||||||
self._sensor_type = sensor_type
|
self._sensor_type = sensor_type
|
||||||
self._name = "{} {}".format(
|
self._metadata = SENSOR_TYPES[self._sensor_type]
|
||||||
self._device.name, SENSOR_TYPES[self._sensor_type][0]
|
self._attr_name = f"{self._device.name} {self._metadata.name}"
|
||||||
)
|
self._device_class = self._metadata.device_class
|
||||||
self._device_class = SENSOR_TYPES[self._sensor_type][1]
|
|
||||||
self._event = {}
|
self._event = {}
|
||||||
self._state = None
|
self._state = None
|
||||||
|
|
||||||
@property
|
|
||||||
def name(self):
|
|
||||||
"""Return the name of the sensor."""
|
|
||||||
return self._name
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_on(self):
|
def is_on(self):
|
||||||
"""Return True if the binary sensor is on."""
|
"""Return True if the binary sensor is on."""
|
||||||
@ -88,7 +101,7 @@ class SkybellBinarySensor(SkybellDevice, BinarySensorEntity):
|
|||||||
"""Get the latest data and updates the state."""
|
"""Get the latest data and updates the state."""
|
||||||
super().update()
|
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"))
|
self._state = bool(event and event.get("id") != self._event.get("id"))
|
||||||
|
|
||||||
|
@ -1,4 +1,8 @@
|
|||||||
"""Switch support for the Skybell HD Doorbell."""
|
"""Switch support for the Skybell HD Doorbell."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from typing import NamedTuple
|
||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components.switch import PLATFORM_SCHEMA, SwitchEntity
|
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
|
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 = {
|
SWITCH_TYPES = {
|
||||||
"do_not_disturb": ["Do Not Disturb"],
|
"do_not_disturb": SkybellSwitchMetadata(
|
||||||
"motion_sensor": ["Motion Sensor"],
|
"Do Not Disturb",
|
||||||
|
),
|
||||||
|
"motion_sensor": SkybellSwitchMetadata(
|
||||||
|
"Motion Sensor",
|
||||||
|
),
|
||||||
}
|
}
|
||||||
|
|
||||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
||||||
@ -44,14 +58,8 @@ class SkybellSwitch(SkybellDevice, SwitchEntity):
|
|||||||
"""Initialize a light for a Skybell device."""
|
"""Initialize a light for a Skybell device."""
|
||||||
super().__init__(device)
|
super().__init__(device)
|
||||||
self._switch_type = switch_type
|
self._switch_type = switch_type
|
||||||
self._name = "{} {}".format(
|
metadata = SWITCH_TYPES[self._switch_type]
|
||||||
self._device.name, SWITCH_TYPES[self._switch_type][0]
|
self._attr_name = f"{self._device.name} {metadata.name}"
|
||||||
)
|
|
||||||
|
|
||||||
@property
|
|
||||||
def name(self):
|
|
||||||
"""Return the name of the sensor."""
|
|
||||||
return self._name
|
|
||||||
|
|
||||||
def turn_on(self, **kwargs):
|
def turn_on(self, **kwargs):
|
||||||
"""Turn on the switch."""
|
"""Turn on the switch."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user