forked from DominikStarke/becker_centralcontrol_has
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlight.py
133 lines (108 loc) · 3.84 KB
/
light.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
"""Representation of a Light in the CentralControl."""
from __future__ import annotations
import logging
from typing import Any
import voluptuous as vol
from homeassistant.components.light import (
PLATFORM_SCHEMA as LIGHT_PLATFORM_SCHEMA,
ColorMode,
LightEntity,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_HOST
from homeassistant.core import HomeAssistant
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .central_control import CentralControl
from .const import BECKER_LIGHT_TYPES, DOMAIN, MANUFACTURER
_LOGGER = logging.getLogger(__name__)
# Validation of the user's configuration
LIGHT_PLATFORM_SCHEMA = LIGHT_PLATFORM_SCHEMA.extend(
{
vol.Required(CONF_HOST): cv.string,
}
)
async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
"""Glue light items to HASS entities."""
central_control: CentralControl = entry.runtime_data
try:
item_list = await central_control.get_item_list(item_type="group")
except TimeoutError:
_LOGGER.error("Failed to get item list")
return
light_list = []
for item in item_list["result"]["item_list"]:
device_class = item["device_type"] in BECKER_LIGHT_TYPES
if device_class is not False:
light_list.append(
BeckerLight(
central_control=central_control,
item=item,
)
)
async_add_entities(light_list)
class BeckerLight(LightEntity):
"""Representation of a Becker light."""
def __init__(
self,
central_control,
item,
) -> None:
"""Initialize the light."""
self._central_control: CentralControl = central_control
self._item = item
@property
def device_info(self) -> DeviceInfo:
"""Return the device information."""
return DeviceInfo(
identifiers={(DOMAIN, self.unique_id)},
manufacturer=MANUFACTURER,
name=self.name,
)
@property
def unique_id(self) -> str:
"""The items unique id."""
return str(self._item["id"])
@property
def name(self) -> str:
"""The items name, "Unknown" if None."""
return self._item.get("name", "Unknown")
@property
def should_poll(self) -> bool:
"""True if the item has the feedback flag."""
if self._item.get("feedback", False) is True:
return True
return False
@property
def color_mode(self) -> ColorMode | str | None:
"""Flag supported color mode."""
if self._item["device_type"] == "dimmer":
return ColorMode.BRIGHTNESS
return ColorMode.ONOFF
async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn the light on."""
await self._central_control.group_send_command(
group_id=int(self.unique_id),
command="switch",
value=1,
)
async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn the the light off."""
await self._central_control.group_send_command(
group_id=int(self.unique_id),
command="switch",
value=0,
)
async def async_added_to_hass(self) -> None:
"""Complete the initialization."""
await self.async_update()
async def async_update(self) -> None:
"""Update brightness."""
state = await self._central_control.get_state(item_id=int(self.unique_id))
if state.get("value", None) is not None:
self._attr_is_on = bool(state["value"])
self._attr_brightness = int(state["value"])
_LOGGER.log(logging.INFO, state)