Add Shelly integration (#39178)
This commit is contained in:
1 parent
6d95ee7a00
commit
ca2bc9906d
16 files changed
+624
No files matched your search
@@ -0,0 +1,71 @@
|
||||
"""Switch for Shelly."""
|
||||
from homeassistant.components.shelly import ShellyBlockEntity
|
||||
from homeassistant.components.switch import SwitchEntity
|
||||
from homeassistant.core import callback
|
||||
|
||||
from .const import DOMAIN
|
||||
|
||||
|
||||
async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||
"""Set up switches for device."""
|
||||
wrapper = hass.data[DOMAIN][config_entry.entry_id]
|
||||
relay_blocks = [block for block in wrapper.device.blocks if block.type == "relay"]
|
||||
|
||||
if not relay_blocks:
|
||||
return
|
||||
|
||||
if wrapper.model == "SHSW-25" and wrapper.device.settings["mode"] != "relay":
|
||||
return
|
||||
|
||||
multiple_blocks = len(relay_blocks) > 1
|
||||
async_add_entities(
|
||||
RelaySwitch(wrapper, block, multiple_blocks=multiple_blocks)
|
||||
for block in relay_blocks
|
||||
)
|
||||
|
||||
|
||||
class RelaySwitch(ShellyBlockEntity, SwitchEntity):
|
||||
"""Switch that controls a relay block on Shelly devices."""
|
||||
|
||||
def __init__(self, *args, multiple_blocks) -> None:
|
||||
"""Initialize relay switch."""
|
||||
super().__init__(*args)
|
||||
self.multiple_blocks = multiple_blocks
|
||||
self.control_result = None
|
||||
|
||||
@property
|
||||
def is_on(self) -> bool:
|
||||
"""If switch is on."""
|
||||
if self.control_result:
|
||||
return self.control_result["ison"]
|
||||
|
||||
return self.block.output
|
||||
|
||||
@property
|
||||
def device_info(self):
|
||||
"""Device info."""
|
||||
if not self.multiple_blocks:
|
||||
return super().device_info
|
||||
|
||||
# If a device has multiple relays, we want to expose as separate device
|
||||
return {
|
||||
"name": self.name,
|
||||
"identifiers": {(DOMAIN, self.wrapper.mac, self.block.index)},
|
||||
"via_device": (DOMAIN, self.wrapper.mac),
|
||||
}
|
||||
|
||||
async def async_turn_on(self, **kwargs):
|
||||
"""Turn on relay."""
|
||||
self.control_result = await self.block.turn_on()
|
||||
self.async_write_ha_state()
|
||||
|
||||
async def async_turn_off(self, **kwargs):
|
||||
"""Turn off relay."""
|
||||
self.control_result = await self.block.turn_off()
|
||||
self.async_write_ha_state()
|
||||
|
||||
@callback
|
||||
def _update_callback(self):
|
||||
"""When device updates, clear control result that overrides state."""
|
||||
self.control_result = None
|
||||
super()._update_callback()
|
||||
Reference in new issue
Block a user