Add devices to LiteJet (#88406)

This commit is contained in:
Jon Caruana 2023-02-22 10:08:11 -08:00 committed by GitHub
parent 0c03862a70
commit d41facf750
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 45 additions and 32 deletions

View File

@ -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)

View File

@ -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

View File

@ -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

View File

@ -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,
)

View File

@ -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