forked from ajmarks/ha_components
-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- added initial version of the erd light entity
- Loading branch information
Showing
6 changed files
with
108 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import logging | ||
|
||
from gehomesdk import ErdCodeType | ||
from homeassistant.components.light import ( | ||
ATTR_BRIGHTNESS, | ||
COLOR_MODE_BRIGHTNESS, | ||
SUPPORT_BRIGHTNESS, | ||
LightEntity | ||
) | ||
|
||
from ...devices import ApplianceApi | ||
from .ge_erd_entity import GeErdEntity | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
def to_ge_level(level): | ||
"""Convert the given Home Assistant light level (0-255) to GE (0-100).""" | ||
return int(round((level * 100) / 255)) | ||
|
||
def to_hass_level(level): | ||
"""Convert the given GE (0-100) light level to Home Assistant (0-255).""" | ||
return int((level * 255) // 100) | ||
|
||
class GeErdLight(GeErdEntity, LightEntity): | ||
"""Lights for ERD codes.""" | ||
|
||
def __init__(self, api: ApplianceApi, erd_code: ErdCodeType, erd_override: str = None, color_mode = COLOR_MODE_BRIGHTNESS): | ||
super().__init__(api, erd_code, erd_override) | ||
self._attr_color_mode = color_mode | ||
|
||
@property | ||
def supported_features(self): | ||
"""Flag supported features.""" | ||
return SUPPORT_BRIGHTNESS | ||
|
||
@property | ||
def supported_color_modes(self): | ||
"""Flag supported color modes.""" | ||
return COLOR_MODE_BRIGHTNESS | ||
|
||
@property | ||
def brightness(self): | ||
"""Return the brightness of the light.""" | ||
return to_hass_level(self.appliance.get_erd_value(self.erd_code)) | ||
|
||
async def _set_brightness(self, brightness, **kwargs): | ||
await self.appliance.async_set_erd_value(self.erd_code, to_ge_level(brightness)) | ||
|
||
@property | ||
def is_on(self) -> bool: | ||
"""Return True if light is on.""" | ||
return self.appliance.get_erd_value(self.erd_code) > 0 | ||
|
||
async def async_turn_on(self, **kwargs): | ||
"""Turn the light on.""" | ||
brightness = kwargs.pop(ATTR_BRIGHTNESS, 255) | ||
|
||
_LOGGER.debug(f"Turning on {self.unique_id}") | ||
await self._set_brightness(brightness, kwargs) | ||
|
||
async def async_turn_off(self, **kwargs): | ||
"""Turn the light off.""" | ||
_LOGGER.debug(f"Turning off {self.unique_id}") | ||
await self._set_brightness(0, kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
"""GE Home Select Entities""" | ||
import async_timeout | ||
import logging | ||
from typing import Callable | ||
|
||
from homeassistant.config_entries import ConfigEntry | ||
from homeassistant.core import HomeAssistant | ||
|
||
from .const import DOMAIN | ||
from .entities import GeErdLight | ||
from .update_coordinator import GeHomeUpdateCoordinator | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
async def async_setup_entry( | ||
hass: HomeAssistant, config_entry: ConfigEntry, async_add_entities: Callable | ||
): | ||
"""GE Home lights.""" | ||
_LOGGER.debug("Adding GE Home lights") | ||
coordinator: GeHomeUpdateCoordinator = hass.data[DOMAIN][config_entry.entry_id] | ||
|
||
# This should be a NOP, but let's be safe | ||
with async_timeout.timeout(20): | ||
await coordinator.initialization_future | ||
_LOGGER.debug("Coordinator init future finished") | ||
|
||
apis = list(coordinator.appliance_apis.values()) | ||
_LOGGER.debug(f"Found {len(apis):d} appliance APIs") | ||
entities = [ | ||
entity | ||
for api in apis | ||
for entity in api.entities | ||
if isinstance(entity, GeErdLight) | ||
and entity.erd_code in api.appliance._property_cache | ||
] | ||
_LOGGER.debug(f"Found {len(entities):d} lights") | ||
async_add_entities(entities) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters