hass-core/homeassistant/components/refoss/switch.py
ashionky 102c7f1959
Add Refoss integration (#100573)
* refoss

* refoss

* refoss

* refoss

* refoss modify

* ip

* 8.22

* format

* format

* format

* bugfix

* test

* test

* test

* test

* test

* test

* 9.1

* refosss

* refoss

* refoss

* refoss

* refoss

* refoss

* refoss

* refoss

* test

* requirements_test_all.txt

* codeowners

* refoss

* Review feedback repair

* strings

* refoss

* refoss

* refoss

* 1.1.1

* 1.1.2

* refoss

* refoss

* refoss.1.1.7

* refoss-gree

* 1.1.7

* test

* refoss

* test refoss

* test refoss

* refoss-test

* refoss

* refoss

* test

* test

* refoss

* CODEOWNERS

* fix

* Update homeassistant/components/refoss/__init__.py

---------

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
2023-12-22 13:18:32 +01:00

69 lines
2.2 KiB
Python

"""Switch for Refoss."""
from __future__ import annotations
from typing import Any
from refoss_ha.controller.toggle import ToggleXMix
from homeassistant.components.switch import SwitchEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import COORDINATORS, DISPATCH_DEVICE_DISCOVERED, DOMAIN
from .entity import RefossEntity
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the Refoss device from a config entry."""
@callback
def init_device(coordinator):
"""Register the device."""
device = coordinator.device
if not isinstance(device, ToggleXMix):
return
new_entities = []
for channel in device.channels:
entity = RefossSwitch(coordinator=coordinator, channel=channel)
new_entities.append(entity)
async_add_entities(new_entities)
for coordinator in hass.data[DOMAIN][COORDINATORS]:
init_device(coordinator)
config_entry.async_on_unload(
async_dispatcher_connect(hass, DISPATCH_DEVICE_DISCOVERED, init_device)
)
class RefossSwitch(RefossEntity, SwitchEntity):
"""Refoss Switch Device."""
@property
def is_on(self) -> bool | None:
"""Return true if switch is on."""
return self.coordinator.device.is_on(channel=self.channel_id)
async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn the switch on."""
await self.coordinator.device.async_turn_on(self.channel_id)
self.async_write_ha_state()
async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn the switch off."""
await self.coordinator.device.async_turn_off(self.channel_id)
self.async_write_ha_state()
async def async_toggle(self, **kwargs: Any) -> None:
"""Toggle the switch."""
await self.coordinator.device.async_toggle(channel=self.channel_id)
self.async_write_ha_state()