From 37a45b1a927254dac0e87fe8f6a030d17a3e2ea8 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Mon, 19 Jan 2026 10:26:21 +0100 Subject: [PATCH] Use shorthand attributes in qwikswitch sensor/binary_sensor (#161209) --- .../components/qwikswitch/binary_sensor.py | 26 ++++------------ homeassistant/components/qwikswitch/entity.py | 15 ++-------- homeassistant/components/qwikswitch/sensor.py | 30 ++++++------------- 3 files changed, 18 insertions(+), 53 deletions(-) diff --git a/homeassistant/components/qwikswitch/binary_sensor.py b/homeassistant/components/qwikswitch/binary_sensor.py index 6033b5f584a..25a9917297e 100644 --- a/homeassistant/components/qwikswitch/binary_sensor.py +++ b/homeassistant/components/qwikswitch/binary_sensor.py @@ -3,6 +3,7 @@ from __future__ import annotations import logging +from typing import Any from pyqwikswitch.qwikswitch import SENSORS @@ -39,9 +40,7 @@ async def async_setup_platform( class QSBinarySensor(QSEntity, BinarySensorEntity): """Sensor based on a Qwikswitch relay/dimmer module.""" - _val = False - - def __init__(self, sensor): + def __init__(self, sensor: dict[str, Any]) -> None: """Initialize the sensor.""" super().__init__(sensor["id"], sensor["name"]) @@ -50,7 +49,9 @@ class QSBinarySensor(QSEntity, BinarySensorEntity): self._decode, _ = SENSORS[sensor_type] self._invert = not sensor.get("invert", False) - self._class = sensor.get("class", "door") + self._attr_is_on = not self._invert + self._attr_device_class = sensor.get("class", BinarySensorDeviceClass.DOOR) + self._attr_unique_id = f"qs{self.qsid}:{self.channel}" @callback def update_packet(self, packet): @@ -65,20 +66,5 @@ class QSBinarySensor(QSEntity, BinarySensorEntity): packet, ) if val is not None: - self._val = bool(val) + self._attr_is_on = bool(val) == self._invert self.async_write_ha_state() - - @property - def is_on(self): - """Check if device is on (non-zero).""" - return self._val == self._invert - - @property - def unique_id(self): - """Return a unique identifier for this sensor.""" - return f"qs{self.qsid}:{self.channel}" - - @property - def device_class(self) -> BinarySensorDeviceClass: - """Return the class of this sensor.""" - return self._class diff --git a/homeassistant/components/qwikswitch/entity.py b/homeassistant/components/qwikswitch/entity.py index 4df52fda2c4..e163b4708a7 100644 --- a/homeassistant/components/qwikswitch/entity.py +++ b/homeassistant/components/qwikswitch/entity.py @@ -15,21 +15,12 @@ class QSEntity(Entity): _attr_should_poll = False - def __init__(self, qsid, name): + def __init__(self, qsid: str, name: str) -> None: """Initialize the QSEntity.""" - self._name = name + self._attr_name = name + self._attr_unique_id = f"qs{qsid}" self.qsid = qsid - @property - def name(self): - """Return the name of the sensor.""" - return self._name - - @property - def unique_id(self): - """Return a unique identifier for this sensor.""" - return f"qs{self.qsid}" - @callback def update_packet(self, packet): """Receive update packet from QSUSB. Match dispather_send signature.""" diff --git a/homeassistant/components/qwikswitch/sensor.py b/homeassistant/components/qwikswitch/sensor.py index 3a79dd0af98..8a3a4f01032 100644 --- a/homeassistant/components/qwikswitch/sensor.py +++ b/homeassistant/components/qwikswitch/sensor.py @@ -37,21 +37,24 @@ async def async_setup_platform( class QSSensor(QSEntity, SensorEntity): """Sensor based on a Qwikswitch relay/dimmer module.""" - _val: Any | None = None - - def __init__(self, sensor): + def __init__(self, sensor: dict[str, Any]) -> None: """Initialize the sensor.""" super().__init__(sensor["id"], sensor["name"]) self.channel = sensor["channel"] sensor_type = sensor["type"] - self._decode, self.unit = SENSORS[sensor_type] + self._attr_unique_id = f"qs{self.qsid}:{self.channel}" + + decode, unit = SENSORS[sensor_type] # this cannot happen because it only happens in bool and this should be redirected to binary_sensor - assert not isinstance(self.unit, type), ( + assert not isinstance(unit, type), ( f"boolean sensor id={sensor['id']} name={sensor['name']}" ) + self._decode = decode + self._attr_native_unit_of_measurement = unit + @callback def update_packet(self, packet): """Receive update packet from QSUSB.""" @@ -65,20 +68,5 @@ class QSSensor(QSEntity, SensorEntity): packet, ) if val is not None: - self._val = val + self._attr_native_value = str(val) self.async_write_ha_state() - - @property - def native_value(self): - """Return the value of the sensor.""" - return None if self._val is None else str(self._val) - - @property - def unique_id(self): - """Return a unique identifier for this sensor.""" - return f"qs{self.qsid}:{self.channel}" - - @property - def native_unit_of_measurement(self): - """Return the unit the value is expressed in.""" - return self.unit