From d41facf750c7c40a681ac868cf6d70c98205402a Mon Sep 17 00:00:00 2001 From: Jon Caruana Date: Wed, 22 Feb 2023 10:08:11 -0800 Subject: [PATCH] Add devices to LiteJet (#88406) --- homeassistant/components/litejet/light.py | 15 +++++++--- homeassistant/components/litejet/scene.py | 23 ++++++++------- homeassistant/components/litejet/switch.py | 33 ++++++++++++---------- tests/components/litejet/__init__.py | 2 +- tests/components/litejet/test_scene.py | 4 +-- 5 files changed, 45 insertions(+), 32 deletions(-) diff --git a/homeassistant/components/litejet/light.py b/homeassistant/components/litejet/light.py index 09855a4d0d5..9b771bdc035 100644 --- a/homeassistant/components/litejet/light.py +++ b/homeassistant/components/litejet/light.py @@ -15,6 +15,7 @@ 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 import DeviceInfo from homeassistant.helpers.entity_platform import AddEntitiesCallback from .const import CONF_DEFAULT_TRANSITION, DOMAIN @@ -46,19 +47,25 @@ class LiteJetLight(LightEntity): _attr_should_poll = False _attr_supported_color_modes = {ColorMode.BRIGHTNESS} _attr_supported_features = LightEntityFeature.TRANSITION + _attr_has_entity_name = True + _attr_name = None def __init__( - self, config_entry: ConfigEntry, litejet: LiteJet, index: int, name: str + self, config_entry: ConfigEntry, system: LiteJet, index: int, name: str ) -> None: """Initialize a LiteJet light.""" self._config_entry = config_entry - self._lj = litejet + self._lj = system self._index = index self._attr_brightness = 0 self._attr_is_on = False - self._attr_name = name self._attr_unique_id = f"{config_entry.entry_id}_{index}" self._attr_extra_state_attributes = {ATTR_NUMBER: self._index} + self._attr_device_info = DeviceInfo( + identifiers={(DOMAIN, f"{config_entry.entry_id}_light_{index}")}, + name=name, + via_device=(DOMAIN, f"{config_entry.entry_id}_mcp"), + ) async def async_added_to_hass(self) -> None: """Run when this Entity has been added to HA.""" @@ -71,7 +78,7 @@ class LiteJetLight(LightEntity): self._lj.unsubscribe(self._on_load_changed) self._lj.unsubscribe(self._on_connected_changed) - def _on_load_changed(self, level) -> None: + def _on_load_changed(self, level: int | None) -> None: """Handle state changes.""" self.schedule_update_ha_state(True) diff --git a/homeassistant/components/litejet/scene.py b/homeassistant/components/litejet/scene.py index c6d4817ccac..83eb2cc5f0b 100644 --- a/homeassistant/components/litejet/scene.py +++ b/homeassistant/components/litejet/scene.py @@ -8,6 +8,7 @@ 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 import DeviceInfo from homeassistant.helpers.entity_platform import AddEntitiesCallback from .const import DOMAIN @@ -37,14 +38,21 @@ async def async_setup_entry( class LiteJetScene(Scene): """Representation of a single LiteJet scene.""" - def __init__( - self, entry_id, lj: LiteJet, i, name # pylint: disable=invalid-name - ) -> None: + _attr_has_entity_name = True + _attr_entity_registry_enabled_default = False + + def __init__(self, entry_id: str, system: LiteJet, i: int, name: str) -> None: """Initialize the scene.""" - self._lj = lj + self._lj = system self._index = i self._attr_unique_id = f"{entry_id}_{i}" self._attr_name = name + self._attr_device_info = DeviceInfo( + identifiers={(DOMAIN, f"{entry_id}_mcp")}, + name="LiteJet", + manufacturer="Centralite", + model="CL24", + ) async def async_added_to_hass(self) -> None: """Run when this Entity has been added to HA.""" @@ -59,7 +67,7 @@ class LiteJetScene(Scene): self.async_write_ha_state() @property - def extra_state_attributes(self): + def extra_state_attributes(self) -> dict[str, Any]: """Return the device-specific state attributes.""" return {ATTR_NUMBER: self._index} @@ -69,8 +77,3 @@ class LiteJetScene(Scene): await self._lj.activate_scene(self._index) except LiteJetError as exc: raise HomeAssistantError() from exc - - @property - def entity_registry_enabled_default(self) -> bool: - """Scenes are only enabled by explicit user choice.""" - return False diff --git a/homeassistant/components/litejet/switch.py b/homeassistant/components/litejet/switch.py index c2dcebd3261..97a51223429 100644 --- a/homeassistant/components/litejet/switch.py +++ b/homeassistant/components/litejet/switch.py @@ -3,10 +3,11 @@ from typing import Any from pylitejet import LiteJet, LiteJetError -from homeassistant.components.switch import SwitchEntity +from homeassistant.components.switch import SwitchDeviceClass, SwitchEntity from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError +from homeassistant.helpers.entity import DeviceInfo from homeassistant.helpers.entity_platform import AddEntitiesCallback from .const import DOMAIN @@ -35,15 +36,27 @@ class LiteJetSwitch(SwitchEntity): """Representation of a single LiteJet switch.""" _attr_should_poll = False + _attr_has_entity_name = True + _attr_entity_registry_enabled_default = False + _attr_device_class = SwitchDeviceClass.SWITCH - def __init__(self, entry_id, lj, i, name): # pylint: disable=invalid-name + def __init__(self, entry_id: str, system: LiteJet, i: int, name: str) -> None: """Initialize a LiteJet switch.""" - self._entry_id = entry_id - self._lj = lj + self._lj = system self._index = i self._attr_is_on = False + self._attr_unique_id = f"{entry_id}_{i}" self._attr_name = name + # Keypad #1 has switches 1-6, #2 has 7-12, ... + keypad_number = int((i - 1) / 6) + 1 + self._attr_device_info = DeviceInfo( + identifiers={(DOMAIN, f"{entry_id}_keypad_{keypad_number}")}, + name=f"Keypad #{keypad_number}", + manufacturer="Centralite", + via_device=(DOMAIN, f"{entry_id}_mcp"), + ) + 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) @@ -69,12 +82,7 @@ class LiteJetSwitch(SwitchEntity): self.async_write_ha_state() @property - def unique_id(self): - """Return a unique identifier for this switch.""" - return f"{self._entry_id}_{self._index}" - - @property - def extra_state_attributes(self): + def extra_state_attributes(self) -> dict[str, Any]: """Return the device-specific state attributes.""" return {ATTR_NUMBER: self._index} @@ -91,8 +99,3 @@ class LiteJetSwitch(SwitchEntity): await self._lj.release_switch(self._index) except LiteJetError as exc: raise HomeAssistantError() from exc - - @property - def entity_registry_enabled_default(self) -> bool: - """Switches are only enabled by explicit user choice.""" - return False diff --git a/tests/components/litejet/__init__.py b/tests/components/litejet/__init__.py index e0304c21617..1ba39e46bf3 100644 --- a/tests/components/litejet/__init__.py +++ b/tests/components/litejet/__init__.py @@ -41,7 +41,7 @@ async def async_init_integration( scene.DOMAIN, DOMAIN, f"{entry.entry_id}_1", - suggested_object_id="mock_scene_1", + suggested_object_id="litejet_mock_scene_1", disabled_by=None, ) diff --git a/tests/components/litejet/test_scene.py b/tests/components/litejet/test_scene.py index a430fc6387f..d1316d81bbe 100644 --- a/tests/components/litejet/test_scene.py +++ b/tests/components/litejet/test_scene.py @@ -11,9 +11,9 @@ from homeassistant.helpers import entity_registry as er from . import async_init_integration -ENTITY_SCENE = "scene.mock_scene_1" +ENTITY_SCENE = "scene.litejet_mock_scene_1" ENTITY_SCENE_NUMBER = 1 -ENTITY_OTHER_SCENE = "scene.mock_scene_2" +ENTITY_OTHER_SCENE = "scene.litejet_mock_scene_2" ENTITY_OTHER_SCENE_NUMBER = 2