mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 05:07:41 +00:00
Add switch platform to Comelit SmartHome (#102233)
* Add switch platform to Comelit SmartHome * add device class only when needed * apply review comment * small cleanup for light platform * update functions description * fix list of values
This commit is contained in:
parent
61104dd726
commit
b9c7613774
@ -178,6 +178,7 @@ omit =
|
|||||||
homeassistant/components/comelit/cover.py
|
homeassistant/components/comelit/cover.py
|
||||||
homeassistant/components/comelit/coordinator.py
|
homeassistant/components/comelit/coordinator.py
|
||||||
homeassistant/components/comelit/light.py
|
homeassistant/components/comelit/light.py
|
||||||
|
homeassistant/components/comelit/switch.py
|
||||||
homeassistant/components/comfoconnect/fan.py
|
homeassistant/components/comfoconnect/fan.py
|
||||||
homeassistant/components/concord232/alarm_control_panel.py
|
homeassistant/components/concord232/alarm_control_panel.py
|
||||||
homeassistant/components/concord232/binary_sensor.py
|
homeassistant/components/concord232/binary_sensor.py
|
||||||
|
@ -8,7 +8,7 @@ from homeassistant.core import HomeAssistant
|
|||||||
from .const import DEFAULT_PORT, DOMAIN
|
from .const import DEFAULT_PORT, DOMAIN
|
||||||
from .coordinator import ComelitSerialBridge
|
from .coordinator import ComelitSerialBridge
|
||||||
|
|
||||||
PLATFORMS = [Platform.COVER, Platform.LIGHT]
|
PLATFORMS = [Platform.COVER, Platform.LIGHT, Platform.SWITCH]
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
|
@ -49,7 +49,7 @@ class ComelitLightEntity(CoordinatorEntity[ComelitSerialBridge], LightEntity):
|
|||||||
self._device = device
|
self._device = device
|
||||||
super().__init__(coordinator)
|
super().__init__(coordinator)
|
||||||
self._attr_unique_id = f"{config_entry_entry_id}-{device.index}"
|
self._attr_unique_id = f"{config_entry_entry_id}-{device.index}"
|
||||||
self._attr_device_info = self.coordinator.platform_device_info(device)
|
self._attr_device_info = coordinator.platform_device_info(device)
|
||||||
|
|
||||||
async def _light_set_state(self, state: int) -> None:
|
async def _light_set_state(self, state: int) -> None:
|
||||||
"""Set desired light state."""
|
"""Set desired light state."""
|
||||||
@ -61,10 +61,10 @@ class ComelitLightEntity(CoordinatorEntity[ComelitSerialBridge], LightEntity):
|
|||||||
await self._light_set_state(STATE_ON)
|
await self._light_set_state(STATE_ON)
|
||||||
|
|
||||||
async def async_turn_off(self, **kwargs: Any) -> None:
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
||||||
"""Turn the entity off."""
|
"""Turn the light off."""
|
||||||
await self._light_set_state(STATE_OFF)
|
await self._light_set_state(STATE_OFF)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_on(self) -> bool:
|
def is_on(self) -> bool:
|
||||||
"""Return True if entity is on."""
|
"""Return True if light is on."""
|
||||||
return self.coordinator.data[LIGHT][self._device.index].status == STATE_ON
|
return self.coordinator.data[LIGHT][self._device.index].status == STATE_ON
|
||||||
|
79
homeassistant/components/comelit/switch.py
Normal file
79
homeassistant/components/comelit/switch.py
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
"""Support for switches."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
from aiocomelit import ComelitSerialBridgeObject
|
||||||
|
from aiocomelit.const import IRRIGATION, OTHER
|
||||||
|
|
||||||
|
from homeassistant.components.switch import SwitchDeviceClass, SwitchEntity
|
||||||
|
from homeassistant.config_entries import ConfigEntry
|
||||||
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
|
|
||||||
|
from .const import DOMAIN, STATE_OFF, STATE_ON
|
||||||
|
from .coordinator import ComelitSerialBridge
|
||||||
|
|
||||||
|
|
||||||
|
async def async_setup_entry(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
config_entry: ConfigEntry,
|
||||||
|
async_add_entities: AddEntitiesCallback,
|
||||||
|
) -> None:
|
||||||
|
"""Set up Comelit switches."""
|
||||||
|
|
||||||
|
coordinator: ComelitSerialBridge = hass.data[DOMAIN][config_entry.entry_id]
|
||||||
|
|
||||||
|
# Use config_entry.entry_id as base for unique_id because no serial number or mac is available
|
||||||
|
async_add_entities(
|
||||||
|
ComelitSwitchEntity(coordinator, device, config_entry.entry_id)
|
||||||
|
for device in (
|
||||||
|
coordinator.data[OTHER].values() + coordinator.data[IRRIGATION].values()
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class ComelitSwitchEntity(CoordinatorEntity[ComelitSerialBridge], SwitchEntity):
|
||||||
|
"""Switch device."""
|
||||||
|
|
||||||
|
_attr_has_entity_name = True
|
||||||
|
_attr_name = None
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
coordinator: ComelitSerialBridge,
|
||||||
|
device: ComelitSerialBridgeObject,
|
||||||
|
config_entry_entry_id: str,
|
||||||
|
) -> None:
|
||||||
|
"""Init switch entity."""
|
||||||
|
self._api = coordinator.api
|
||||||
|
self._device = device
|
||||||
|
super().__init__(coordinator)
|
||||||
|
self._attr_unique_id = f"{config_entry_entry_id}-{device.index}"
|
||||||
|
self._attr_device_info = coordinator.platform_device_info(device)
|
||||||
|
if device.type == OTHER:
|
||||||
|
self._attr_device_class = SwitchDeviceClass.OUTLET
|
||||||
|
|
||||||
|
async def _switch_set_state(self, state: int) -> None:
|
||||||
|
"""Set desired switch state."""
|
||||||
|
await self.coordinator.api.set_device_status(
|
||||||
|
self._device.type, self._device.index, state
|
||||||
|
)
|
||||||
|
await self.coordinator.async_request_refresh()
|
||||||
|
|
||||||
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
||||||
|
"""Turn the switch on."""
|
||||||
|
await self._switch_set_state(STATE_ON)
|
||||||
|
|
||||||
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
||||||
|
"""Turn the switch off."""
|
||||||
|
await self._switch_set_state(STATE_OFF)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_on(self) -> bool:
|
||||||
|
"""Return True if switch is on."""
|
||||||
|
return (
|
||||||
|
self.coordinator.data[self._device.type][self._device.index].status
|
||||||
|
== STATE_ON
|
||||||
|
)
|
Loading…
x
Reference in New Issue
Block a user