mirror of
https://github.com/home-assistant/core.git
synced 2025-07-08 13:57:10 +00:00
Add basic binary_sensor support to Shelly (#39365)
This commit is contained in:
parent
ef13da5555
commit
4aa8b6cad8
@ -754,6 +754,7 @@ omit =
|
||||
homeassistant/components/shiftr/*
|
||||
homeassistant/components/shodan/sensor.py
|
||||
homeassistant/components/shelly/__init__.py
|
||||
homeassistant/components/shelly/binary_sensor.py
|
||||
homeassistant/components/shelly/light.py
|
||||
homeassistant/components/shelly/sensor.py
|
||||
homeassistant/components/shelly/switch.py
|
||||
|
@ -20,7 +20,7 @@ from homeassistant.helpers import (
|
||||
|
||||
from .const import DOMAIN
|
||||
|
||||
PLATFORMS = ["switch", "light", "sensor"]
|
||||
PLATFORMS = ["binary_sensor", "light", "sensor", "switch"]
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
|
72
homeassistant/components/shelly/binary_sensor.py
Normal file
72
homeassistant/components/shelly/binary_sensor.py
Normal file
@ -0,0 +1,72 @@
|
||||
"""Binary sensor for Shelly."""
|
||||
import aioshelly
|
||||
|
||||
from homeassistant.components.binary_sensor import (
|
||||
DEVICE_CLASS_MOISTURE,
|
||||
DEVICE_CLASS_OPENING,
|
||||
DEVICE_CLASS_SMOKE,
|
||||
BinarySensorEntity,
|
||||
)
|
||||
|
||||
from . import ShellyBlockEntity, ShellyDeviceWrapper
|
||||
from .const import DOMAIN
|
||||
|
||||
SENSORS = {
|
||||
"dwIsOpened": DEVICE_CLASS_OPENING,
|
||||
"flood": DEVICE_CLASS_MOISTURE,
|
||||
"overpower": None,
|
||||
"smoke": DEVICE_CLASS_SMOKE,
|
||||
}
|
||||
|
||||
|
||||
async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||
"""Set up sensors for device."""
|
||||
wrapper = hass.data[DOMAIN][config_entry.entry_id]
|
||||
sensors = []
|
||||
|
||||
for block in wrapper.device.blocks:
|
||||
for attr in SENSORS:
|
||||
if not hasattr(block, attr):
|
||||
continue
|
||||
|
||||
sensors.append(ShellySensor(wrapper, block, attr))
|
||||
|
||||
if sensors:
|
||||
async_add_entities(sensors)
|
||||
|
||||
|
||||
class ShellySensor(ShellyBlockEntity, BinarySensorEntity):
|
||||
"""Switch that controls a relay block on Shelly devices."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
wrapper: ShellyDeviceWrapper,
|
||||
block: aioshelly.Block,
|
||||
attribute: str,
|
||||
) -> None:
|
||||
"""Initialize sensor."""
|
||||
super().__init__(wrapper, block)
|
||||
self.attribute = attribute
|
||||
device_class = SENSORS[attribute]
|
||||
|
||||
self._device_class = device_class
|
||||
|
||||
@property
|
||||
def unique_id(self):
|
||||
"""Return unique ID of entity."""
|
||||
return f"{super().unique_id}-{self.attribute}"
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""Name of sensor."""
|
||||
return f"{self.wrapper.name} - {self.attribute}"
|
||||
|
||||
@property
|
||||
def is_on(self):
|
||||
"""Return true if sensor state is 1."""
|
||||
return bool(getattr(self.block, self.attribute))
|
||||
|
||||
@property
|
||||
def device_class(self):
|
||||
"""Device class of sensor."""
|
||||
return self._device_class
|
Loading…
x
Reference in New Issue
Block a user