Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add server side events to Smlight integration #125553

Merged
merged 5 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add switch events for settings changes
  • Loading branch information
tl-sl authored and darkxst committed Sep 9, 2024
commit 14cf2548d148596747d7ed4b3497c9c25b782c46
8 changes: 8 additions & 0 deletions homeassistant/components/smlight/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from dataclasses import dataclass

from pysmlight import Api2, Info, Sensors
from pysmlight.const import Settings, SettingsProp
from pysmlight.exceptions import SmlightAuthError, SmlightConnectionError

from homeassistant.config_entries import ConfigEntry
Expand Down Expand Up @@ -82,6 +83,13 @@ async def _async_setup(self) -> None:
translation_key="unsupported_firmware",
)

def update_setting(self, setting: Settings, value: bool | int) -> None:
"""Update the sensor value from event."""
prop = SettingsProp[setting.name].value
setattr(self.data.sensors, prop, value)

self.async_set_updated_data(self.data)

async def _async_update_data(self) -> SmData:
"""Fetch data from the SMLIGHT device."""
try:
Expand Down
27 changes: 19 additions & 8 deletions homeassistant/components/smlight/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import logging
from typing import Any

from pysmlight import Sensors
from pysmlight import Sensors, SettingsEvent
from pysmlight.const import Settings

from homeassistant.components.switch import (
Expand All @@ -16,7 +16,7 @@
SwitchEntityDescription,
)
from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from . import SmConfigEntry
Expand Down Expand Up @@ -86,22 +86,33 @@ def __init__(

self._page, self._toggle = description.setting.value

async def async_added_to_hass(self) -> None:
"""Run when entity about to be added to hass."""
await super().async_added_to_hass()
self.async_on_remove(
self.coordinator.client.sse.register_settings_cb(
self.entity_description.setting, self.event_callback
)
)

async def set_smlight(self, state: bool) -> None:
"""Set the state on SLZB device."""
await self.coordinator.client.set_toggle(self._page, self._toggle, state)

@callback
def event_callback(self, event: SettingsEvent) -> None:
"""Handle switch events from the SLZB device."""
if event.setting is not None:
tl-sl marked this conversation as resolved.
Show resolved Hide resolved
self.coordinator.update_setting(
self.entity_description.setting, event.setting[self._toggle]
)

async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn the switch on."""
self._attr_is_on = True
self.async_write_ha_state()

await self.set_smlight(True)

async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn the switch off."""
self._attr_is_on = False
self.async_write_ha_state()

await self.set_smlight(False)

@property
Expand Down