mirror of
https://github.com/home-assistant/core.git
synced 2026-02-15 07:36:16 +00:00
Use shorthand attributes in qwikswitch sensor/binary_sensor (#161209)
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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."""
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user