diff --git a/custom_components/ge_home/devices/fridge.py b/custom_components/ge_home/devices/fridge.py index 6fa4cea..43a1936 100644 --- a/custom_components/ge_home/devices/fridge.py +++ b/custom_components/ge_home/devices/fridge.py @@ -24,6 +24,7 @@ from ..entities import ( GeErdBinarySensor, GeErdSwitch, GeErdSelect, + GeErdLight, GeFridge, GeFreezer, GeDispenser, @@ -83,7 +84,7 @@ class FridgeApi(ApplianceApi): if(ice_bucket_status and ice_bucket_status.is_present_fridge): fridge_entities.append(GeErdPropertySensor(self, ErdCode.ICE_MAKER_BUCKET_STATUS, "state_full_fridge")) if(interior_light and interior_light != 255): - fridge_entities.append(GeErdSensor(self, ErdCode.INTERIOR_LIGHT)) + fridge_entities.append(GeErdLight(self, ErdCode.INTERIOR_LIGHT)) if(proximity_light and proximity_light != ErdOnOff.NA): fridge_entities.append(GeErdSwitch(self, ErdCode.PROXIMITY_LIGHT, ErdOnOffBoolConverter(), icon_on_override="mdi:lightbulb-on", icon_off_override="mdi:lightbulb")) if(convertable_drawer and convertable_drawer != ErdConvertableDrawerMode.NA): diff --git a/custom_components/ge_home/entities/common/__init__.py b/custom_components/ge_home/entities/common/__init__.py index 0094d25..7db556b 100644 --- a/custom_components/ge_home/entities/common/__init__.py +++ b/custom_components/ge_home/entities/common/__init__.py @@ -5,6 +5,7 @@ from .ge_erd_entity import GeErdEntity from .ge_erd_binary_sensor import GeErdBinarySensor from .ge_erd_property_binary_sensor import GeErdPropertyBinarySensor from .ge_erd_sensor import GeErdSensor +from .ge_erd_light import GeErdLight from .ge_erd_timer_sensor import GeErdTimerSensor from .ge_erd_property_sensor import GeErdPropertySensor from .ge_erd_switch import GeErdSwitch diff --git a/custom_components/ge_home/entities/common/ge_erd_light.py b/custom_components/ge_home/entities/common/ge_erd_light.py new file mode 100644 index 0000000..b07281f --- /dev/null +++ b/custom_components/ge_home/entities/common/ge_erd_light.py @@ -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) diff --git a/custom_components/ge_home/light.py b/custom_components/ge_home/light.py new file mode 100644 index 0000000..310cabf --- /dev/null +++ b/custom_components/ge_home/light.py @@ -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) diff --git a/custom_components/ge_home/manifest.json b/custom_components/ge_home/manifest.json index d9a461e..b229ce3 100644 --- a/custom_components/ge_home/manifest.json +++ b/custom_components/ge_home/manifest.json @@ -3,7 +3,7 @@ "name": "GE Home (SmartHQ)", "config_flow": true, "documentation": "https://github.com/simbaja/ha_gehome", - "requirements": ["gehomesdk==0.4.9","magicattr==0.1.5"], + "requirements": ["gehomesdk==0.4.11","magicattr==0.1.5"], "codeowners": ["@simbaja"], "version": "0.5.0" } diff --git a/custom_components/ge_home/update_coordinator.py b/custom_components/ge_home/update_coordinator.py index 126a320..b3ee44b 100644 --- a/custom_components/ge_home/update_coordinator.py +++ b/custom_components/ge_home/update_coordinator.py @@ -34,7 +34,7 @@ from .const import ( ) from .devices import ApplianceApi, get_appliance_api_type -PLATFORMS = ["binary_sensor", "sensor", "switch", "water_heater", "select", "climate"] +PLATFORMS = ["binary_sensor", "sensor", "switch", "water_heater", "select", "climate", "light"] _LOGGER = logging.getLogger(__name__)