core/homeassistant/components/lookin/light.py

76 lines
2.2 KiB
Python
Raw Normal View History

2022-01-14 03:07:02 +00:00
"""The lookin integration light platform."""
from __future__ import annotations
import logging
2022-01-15 00:22:06 +00:00
from typing import Any
2022-01-14 03:07:02 +00:00
2022-04-23 19:17:51 +00:00
from homeassistant.components.light import ColorMode, LightEntity
2022-01-14 03:07:02 +00:00
from homeassistant.config_entries import ConfigEntry
2022-01-15 00:22:06 +00:00
from homeassistant.const import Platform
2022-01-14 03:07:02 +00:00
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
2022-01-15 00:22:06 +00:00
from .const import DOMAIN, TYPE_TO_PLATFORM
from .entity import LookinPowerPushRemoteEntity
2022-01-14 03:07:02 +00:00
from .models import LookinData
LOGGER = logging.getLogger(__name__)
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the light platform for lookin from a config entry."""
lookin_data: LookinData = hass.data[DOMAIN][config_entry.entry_id]
entities = []
for remote in lookin_data.devices:
2022-01-15 00:22:06 +00:00
if TYPE_TO_PLATFORM.get(remote["Type"]) != Platform.LIGHT:
2022-01-14 03:07:02 +00:00
continue
uuid = remote["UUID"]
2022-01-15 00:22:06 +00:00
coordinator = lookin_data.device_coordinators[uuid]
device = coordinator.data
2022-01-14 03:07:02 +00:00
entities.append(
LookinLightEntity(
2022-01-15 00:22:06 +00:00
coordinator=coordinator,
2022-01-14 03:07:02 +00:00
uuid=uuid,
device=device,
lookin_data=lookin_data,
)
)
async_add_entities(entities)
2022-01-15 00:22:06 +00:00
class LookinLightEntity(LookinPowerPushRemoteEntity, LightEntity):
2022-01-14 03:07:02 +00:00
"""A lookin IR controlled light."""
2022-04-23 19:17:51 +00:00
_attr_supported_color_modes = {ColorMode.ONOFF}
_attr_color_mode = ColorMode.ONOFF
2022-01-14 03:07:02 +00:00
async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn on the light."""
await self._async_send_command(self._power_on_command)
self._attr_is_on = True
self.async_write_ha_state()
async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn off the light."""
await self._async_send_command(self._power_off_command)
self._attr_is_on = False
self.async_write_ha_state()
def _update_from_status(self, status: str) -> None:
"""Update media property from status.
1000
0 - 0/1 on/off
"""
if len(status) != 4:
return
state = status[0]
self._attr_is_on = state == "1"