diff --git a/homeassistant/components/litejet/__init__.py b/homeassistant/components/litejet/__init__.py index e2396073fdd..040b8688a42 100644 --- a/homeassistant/components/litejet/__init__.py +++ b/homeassistant/components/litejet/__init__.py @@ -2,7 +2,6 @@ import logging import pylitejet -from serial import SerialException import voluptuous as vol from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry @@ -53,9 +52,16 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: try: system = await pylitejet.open(port) - except SerialException as ex: - _LOGGER.error("Error connecting to the LiteJet MCP at %s", port, exc_info=ex) - raise ConfigEntryNotReady from ex + except pylitejet.LiteJetError as exc: + raise ConfigEntryNotReady from exc + + def handle_connected_changed(connected: bool, reason: str) -> None: + if connected: + _LOGGER.info("Connected") + else: + _LOGGER.warning("Disconnected %s", reason) + + system.on_connected_changed(handle_connected_changed) async def handle_stop(event) -> None: await system.close() diff --git a/homeassistant/components/litejet/light.py b/homeassistant/components/litejet/light.py index 573e2fd5e4f..09855a4d0d5 100644 --- a/homeassistant/components/litejet/light.py +++ b/homeassistant/components/litejet/light.py @@ -1,10 +1,9 @@ """Support for LiteJet lights.""" from __future__ import annotations -import logging from typing import Any -from pylitejet import LiteJet +from pylitejet import LiteJet, LiteJetError from homeassistant.components.light import ( ATTR_BRIGHTNESS, @@ -15,12 +14,11 @@ from homeassistant.components.light import ( ) from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant +from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers.entity_platform import AddEntitiesCallback from .const import CONF_DEFAULT_TRANSITION, DOMAIN -_LOGGER = logging.getLogger(__name__) - ATTR_NUMBER = "number" @@ -66,14 +64,19 @@ class LiteJetLight(LightEntity): """Run when this Entity has been added to HA.""" self._lj.on_load_activated(self._index, self._on_load_changed) self._lj.on_load_deactivated(self._index, self._on_load_changed) + self._lj.on_connected_changed(self._on_connected_changed) async def async_will_remove_from_hass(self) -> None: """Entity being removed from hass.""" self._lj.unsubscribe(self._on_load_changed) + self._lj.unsubscribe(self._on_connected_changed) def _on_load_changed(self, level) -> None: """Handle state changes.""" - _LOGGER.debug("Updating due to notification for %s", self.name) + self.schedule_update_ha_state(True) + + def _on_connected_changed(self, connected: bool, reason: str) -> None: + """Handle connected changes.""" self.schedule_update_ha_state(True) async def async_turn_on(self, **kwargs: Any) -> None: @@ -83,7 +86,10 @@ class LiteJetLight(LightEntity): # LiteJet API will use the per-light default brightness and # transition values programmed in the LiteJet system. if ATTR_BRIGHTNESS not in kwargs and ATTR_TRANSITION not in kwargs: - await self._lj.activate_load(self._index) + try: + await self._lj.activate_load(self._index) + except LiteJetError as exc: + raise HomeAssistantError() from exc return # If either attribute is specified then Home Assistant must @@ -92,21 +98,35 @@ class LiteJetLight(LightEntity): transition = kwargs.get(ATTR_TRANSITION, default_transition) brightness = int(kwargs.get(ATTR_BRIGHTNESS, 255) / 255 * 99) - await self._lj.activate_load_at(self._index, brightness, int(transition)) + try: + await self._lj.activate_load_at(self._index, brightness, int(transition)) + except LiteJetError as exc: + raise HomeAssistantError() from exc async def async_turn_off(self, **kwargs: Any) -> None: """Turn off the light.""" if ATTR_TRANSITION in kwargs: - await self._lj.activate_load_at(self._index, 0, kwargs[ATTR_TRANSITION]) + try: + await self._lj.activate_load_at(self._index, 0, kwargs[ATTR_TRANSITION]) + except LiteJetError as exc: + raise HomeAssistantError() from exc return # If transition attribute is not specified then the simple # deactivate load LiteJet API will use the per-light default # transition value programmed in the LiteJet system. - await self._lj.deactivate_load(self._index) + try: + await self._lj.deactivate_load(self._index) + except LiteJetError as exc: + raise HomeAssistantError() from exc async def async_update(self) -> None: """Retrieve the light's brightness from the LiteJet system.""" + self._attr_available = self._lj.connected + + if not self.available: + return + self._attr_brightness = int( await self._lj.get_load_level(self._index) / 99 * 255 ) diff --git a/homeassistant/components/litejet/manifest.json b/homeassistant/components/litejet/manifest.json index ffc3e214fef..142e790a12a 100644 --- a/homeassistant/components/litejet/manifest.json +++ b/homeassistant/components/litejet/manifest.json @@ -2,7 +2,7 @@ "domain": "litejet", "name": "LiteJet", "documentation": "https://www.home-assistant.io/integrations/litejet", - "requirements": ["pylitejet==0.4.6"], + "requirements": ["pylitejet==0.5.0"], "codeowners": ["@joncar"], "config_flow": true, "iot_class": "local_push", diff --git a/homeassistant/components/litejet/scene.py b/homeassistant/components/litejet/scene.py index 7a37d24230f..01dfc0a3ccd 100644 --- a/homeassistant/components/litejet/scene.py +++ b/homeassistant/components/litejet/scene.py @@ -1,15 +1,19 @@ """Support for LiteJet scenes.""" +import logging from typing import Any -from pylitejet import LiteJet +from pylitejet import LiteJet, LiteJetError from homeassistant.components.scene import Scene from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant +from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers.entity_platform import AddEntitiesCallback from .const import DOMAIN +_LOGGER = logging.getLogger(__name__) + ATTR_NUMBER = "number" @@ -35,20 +39,22 @@ class LiteJetScene(Scene): def __init__(self, entry_id, lj: LiteJet, i, name): # pylint: disable=invalid-name """Initialize the scene.""" - self._entry_id = entry_id self._lj = lj self._index = i - self._name = name + self._attr_unique_id = f"{entry_id}_{i}" + self._attr_name = name - @property - def name(self): - """Return the name of the scene.""" - return self._name + async def async_added_to_hass(self) -> None: + """Run when this Entity has been added to HA.""" + self._lj.on_connected_changed(self._on_connected_changed) - @property - def unique_id(self): - """Return a unique identifier for this scene.""" - return f"{self._entry_id}_{self._index}" + async def async_will_remove_from_hass(self) -> None: + """Entity being removed from hass.""" + self._lj.unsubscribe(self._on_connected_changed) + + def _on_connected_changed(self, connected: bool, reason: str) -> None: + self._attr_available = connected + self.schedule_update_ha_state() @property def extra_state_attributes(self): @@ -57,7 +63,10 @@ class LiteJetScene(Scene): async def async_activate(self, **kwargs: Any) -> None: """Activate the scene.""" - await self._lj.activate_scene(self._index) + try: + await self._lj.activate_scene(self._index) + except LiteJetError as exc: + raise HomeAssistantError() from exc @property def entity_registry_enabled_default(self) -> bool: diff --git a/homeassistant/components/litejet/switch.py b/homeassistant/components/litejet/switch.py index f31d9be04e3..1f010691f02 100644 --- a/homeassistant/components/litejet/switch.py +++ b/homeassistant/components/litejet/switch.py @@ -1,20 +1,18 @@ """Support for LiteJet switch.""" -import logging from typing import Any -from pylitejet import LiteJet +from pylitejet import LiteJet, LiteJetError from homeassistant.components.switch import SwitchEntity from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant +from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers.entity_platform import AddEntitiesCallback from .const import DOMAIN ATTR_NUMBER = "number" -_LOGGER = logging.getLogger(__name__) - async def async_setup_entry( hass: HomeAssistant, @@ -43,44 +41,38 @@ class LiteJetSwitch(SwitchEntity): self._entry_id = entry_id self._lj = lj self._index = i - self._state = False - self._name = name + self._attr_is_on = False + self._attr_name = name async def async_added_to_hass(self) -> None: """Run when this Entity has been added to HA.""" self._lj.on_switch_pressed(self._index, self._on_switch_pressed) self._lj.on_switch_released(self._index, self._on_switch_released) + self._lj.on_connected_changed(self._on_connected_changed) async def async_will_remove_from_hass(self) -> None: """Entity being removed from hass.""" self._lj.unsubscribe(self._on_switch_pressed) self._lj.unsubscribe(self._on_switch_released) + self._lj.unsubscribe(self._on_connected_changed) def _on_switch_pressed(self): - _LOGGER.debug("Updating pressed for %s", self._name) - self._state = True + self._attr_is_on = True self.schedule_update_ha_state() def _on_switch_released(self): - _LOGGER.debug("Updating released for %s", self._name) - self._state = False + self._attr_is_on = False self.schedule_update_ha_state() - @property - def name(self): - """Return the name of the switch.""" - return self._name + def _on_connected_changed(self, connected: bool, reason: str) -> None: + self._attr_available = connected + self.schedule_update_ha_state() @property def unique_id(self): """Return a unique identifier for this switch.""" return f"{self._entry_id}_{self._index}" - @property - def is_on(self): - """Return if the switch is pressed.""" - return self._state - @property def extra_state_attributes(self): """Return the device-specific state attributes.""" @@ -88,11 +80,17 @@ class LiteJetSwitch(SwitchEntity): async def async_turn_on(self, **kwargs: Any) -> None: """Press the switch.""" - await self._lj.press_switch(self._index) + try: + await self._lj.press_switch(self._index) + except LiteJetError as exc: + raise HomeAssistantError() from exc async def async_turn_off(self, **kwargs: Any) -> None: """Release the switch.""" - await self._lj.release_switch(self._index) + try: + await self._lj.release_switch(self._index) + except LiteJetError as exc: + raise HomeAssistantError() from exc @property def entity_registry_enabled_default(self) -> bool: diff --git a/requirements_all.txt b/requirements_all.txt index 57050197174..464504013e1 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -1744,7 +1744,7 @@ pylgnetcast==0.3.7 pylibrespot-java==0.1.1 # homeassistant.components.litejet -pylitejet==0.4.6 +pylitejet==0.5.0 # homeassistant.components.litterrobot pylitterbot==2023.1.1 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 8a68161f77a..ae501ab37d8 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -1251,7 +1251,7 @@ pylaunches==1.3.0 pylibrespot-java==0.1.1 # homeassistant.components.litejet -pylitejet==0.4.6 +pylitejet==0.5.0 # homeassistant.components.litterrobot pylitterbot==2023.1.1