1
0
mirror of https://github.com/home-assistant/core.git synced 2026-02-15 07:36:16 +00:00

Use shorthand attributes in w800rf32 binary_sensor (#161210)

This commit is contained in:
epenet
2026-01-19 08:32:12 +01:00
committed by GitHub
parent e472180fb2
commit 290c2fd5b6

View File

@@ -2,6 +2,7 @@
from __future__ import annotations
from datetime import timedelta
import logging
import voluptuous as vol
@@ -31,7 +32,7 @@ PLATFORM_SCHEMA = BINARY_SENSOR_PLATFORM_SCHEMA.extend(
vol.Required(CONF_DEVICES): {
cv.string: vol.Schema(
{
vol.Optional(CONF_NAME): cv.string,
vol.Required(CONF_NAME): cv.string,
vol.Optional(CONF_DEVICE_CLASS): DEVICE_CLASSES_SCHEMA,
vol.Optional(CONF_OFF_DELAY): vol.All(
cv.time_period, cv.positive_timedelta
@@ -54,7 +55,8 @@ async def async_setup_platform(
binary_sensors = []
# device_id --> "c1 or a3" X10 device. entity (type dictionary)
# --> name, device_class etc
for device_id, entity in config[CONF_DEVICES].items():
devices_config: dict[str, ConfigType] = config[CONF_DEVICES]
for device_id, entity in devices_config.items():
_LOGGER.debug(
"Add %s w800rf32.binary_sensor (class %s)",
entity[CONF_NAME],
@@ -63,7 +65,7 @@ async def async_setup_platform(
device = W800rf32BinarySensor(
device_id,
entity.get(CONF_NAME),
entity[CONF_NAME],
entity.get(CONF_DEVICE_CLASS),
entity.get(CONF_OFF_DELAY),
)
@@ -78,13 +80,21 @@ class W800rf32BinarySensor(BinarySensorEntity):
_attr_should_poll = False
def __init__(self, device_id, name, device_class=None, off_delay=None):
def __init__(
self,
device_id: str,
name: str,
device_class: BinarySensorDeviceClass | None,
off_delay: timedelta | None,
) -> None:
"""Initialize the w800rf32 sensor."""
self._signal = W800RF32_DEVICE.format(device_id)
self._name = name
self._device_class = device_class
self._attr_name = name
self._attr_device_class = device_class
self._attr_is_on = False
self._off_delay = off_delay
self._state = False
self._delay_listener = None
@callback
@@ -93,21 +103,6 @@ class W800rf32BinarySensor(BinarySensorEntity):
self._delay_listener = None
self.update_state(False)
@property
def name(self):
"""Return the device name."""
return self._name
@property
def device_class(self) -> BinarySensorDeviceClass | None:
"""Return the sensor class."""
return self._device_class
@property
def is_on(self):
"""Return true if the sensor state is True."""
return self._state
@callback
def binary_sensor_update(self, event):
"""Call for control updates from the w800rf32 gateway."""
@@ -132,9 +127,9 @@ class W800rf32BinarySensor(BinarySensorEntity):
self.hass, self._off_delay, self._off_delay_listener
)
def update_state(self, state):
def update_state(self, state: bool) -> None:
"""Update the state of the device."""
self._state = state
self._attr_is_on = state
self.async_write_ha_state()
async def async_added_to_hass(self) -> None: