mirror of
https://github.com/andvikt/mega_hacs.git
synced 2025-12-11 09:04:27 +05:00
рефакторинг, конфиг yaml
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
"""Platform for light integration."""
|
||||
import logging
|
||||
import asyncio
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components.light import (
|
||||
@@ -11,17 +10,22 @@ from homeassistant.components.light import (
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import (
|
||||
CONF_NAME,
|
||||
CONF_PLATFORM,
|
||||
CONF_PORT,
|
||||
CONF_UNIQUE_ID,
|
||||
CONF_ID
|
||||
CONF_ID,
|
||||
CONF_DOMAIN,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
from .entities import BaseMegaEntity
|
||||
from .entities import MegaOutPort
|
||||
|
||||
from .hub import MegaD
|
||||
from .const import CONF_DIMMER, CONF_SWITCH
|
||||
|
||||
from .const import (
|
||||
CONF_DIMMER,
|
||||
CONF_SWITCH,
|
||||
DOMAIN,
|
||||
CONF_CUSTOM,
|
||||
CONF_SKIP,
|
||||
)
|
||||
|
||||
lg = logging.getLogger(__name__)
|
||||
|
||||
@@ -47,29 +51,7 @@ PLATFORM_SCHEMA = LIGHT_SCHEMA.extend(
|
||||
|
||||
|
||||
async def async_setup_platform(hass, config, add_entities, discovery_info=None):
|
||||
config.pop(CONF_PLATFORM)
|
||||
ents = []
|
||||
for mid, _config in config.items():
|
||||
for x in _config["dimmer"]:
|
||||
if isinstance(x, int):
|
||||
ent = MegaLight(
|
||||
mega_id=mid, port=x, dimmer=True)
|
||||
else:
|
||||
ent = MegaLight(
|
||||
mega_id=mid, port=x[CONF_PORT], name=x[CONF_NAME], dimmer=True
|
||||
)
|
||||
ents.append(ent)
|
||||
for x in _config["switch"]:
|
||||
if isinstance(x, int):
|
||||
ent = MegaLight(
|
||||
mega_id=mid, port=x, dimmer=False
|
||||
)
|
||||
else:
|
||||
ent = MegaLight(
|
||||
mega_id=mid, port=x[CONF_PORT], name=x[CONF_NAME], dimmer=False
|
||||
)
|
||||
ents.append(ent)
|
||||
add_entities(ents)
|
||||
lg.warning('mega integration does not support yaml for lights, please use UI configuration')
|
||||
return True
|
||||
|
||||
|
||||
@@ -77,77 +59,22 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry, asyn
|
||||
mid = config_entry.data[CONF_ID]
|
||||
hub: MegaD = hass.data['mega'][mid]
|
||||
devices = []
|
||||
|
||||
customize = hass.data.get(DOMAIN, {}).get(CONF_CUSTOM, {})
|
||||
for port, cfg in config_entry.data.get('light', {}).items():
|
||||
port = int(port)
|
||||
c = customize.get(mid, {}).get(port, {})
|
||||
if c.get(CONF_SKIP, False) or c.get(CONF_DOMAIN, 'light') != 'light':
|
||||
continue
|
||||
for data in cfg:
|
||||
hub.lg.debug(f'add light on port %s with data %s', port, data)
|
||||
light = MegaLight(mega_id=mid, port=port, config_entry=config_entry, **data)
|
||||
light = MegaLight(mega=hub, port=port, config_entry=config_entry, **data)
|
||||
devices.append(light)
|
||||
async_add_devices(devices)
|
||||
|
||||
|
||||
class MegaLight(LightEntity, BaseMegaEntity):
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
dimmer=False,
|
||||
*args, **kwargs
|
||||
):
|
||||
super().__init__(
|
||||
*args, **kwargs
|
||||
)
|
||||
self._brightness = None
|
||||
self._is_on = None
|
||||
self.dimmer = dimmer
|
||||
|
||||
@property
|
||||
def brightness(self):
|
||||
if self._brightness is not None:
|
||||
return self._brightness
|
||||
if self._state:
|
||||
return self._state.attributes.get("brightness")
|
||||
class MegaLight(MegaOutPort, LightEntity):
|
||||
|
||||
@property
|
||||
def supported_features(self):
|
||||
return SUPPORT_BRIGHTNESS if self.dimmer else 0
|
||||
|
||||
@property
|
||||
def is_on(self) -> bool:
|
||||
if self._is_on is not None:
|
||||
return self._is_on
|
||||
return self._state == 'ON'
|
||||
|
||||
async def async_turn_on(self, brightness=None, **kwargs) -> None:
|
||||
brightness = brightness or self.brightness or 255
|
||||
if self.dimmer and brightness == 0:
|
||||
cmd = 255
|
||||
elif self.dimmer:
|
||||
cmd = brightness
|
||||
else:
|
||||
cmd = 1
|
||||
if await self.mega.send_command(self.port, f"{self.port}:{cmd}"):
|
||||
self._is_on = True
|
||||
self._brightness = brightness
|
||||
await self.async_update_ha_state()
|
||||
|
||||
async def async_turn_off(self, **kwargs) -> None:
|
||||
|
||||
cmd = "0"
|
||||
|
||||
if await self.mega.send_command(self.port, f"{self.port}:{cmd}"):
|
||||
self._is_on = False
|
||||
await self.async_update_ha_state()
|
||||
|
||||
def _update(self, payload: dict):
|
||||
val = payload.get("value")
|
||||
try:
|
||||
val = int(val)
|
||||
except Exception:
|
||||
pass
|
||||
if isinstance(val, int):
|
||||
self._is_on = val
|
||||
if val > 0:
|
||||
self._brightness = val
|
||||
else:
|
||||
self._is_on = val == 'ON'
|
||||
|
||||
|
||||
Reference in New Issue
Block a user