Skip to content

Commit

Permalink
Fix high load and move scene switch to button
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitry Mamontov committed Feb 7, 2022
1 parent f46b723 commit 141d0e9
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 13 deletions.
14 changes: 14 additions & 0 deletions custom_components/ledfx/button.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import logging

from homeassistant.core import HomeAssistant
from homeassistant.config_entries import ConfigEntry
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.components.button import ButtonEntity
from .core.common import async_setup_ledfx_entities

_LOGGER = logging.getLogger(__name__)

async def async_setup_entry(
hass: HomeAssistant, config_entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
await async_setup_ledfx_entities(hass, config_entry, async_add_entities, ButtonEntity)
17 changes: 9 additions & 8 deletions custom_components/ledfx/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from homeassistant.components.number import NumberEntity
from homeassistant.components.number.const import DEFAULT_MAX_VALUE, DEFAULT_MIN_VALUE
from homeassistant.components.select import SelectEntity
from homeassistant.components.button import ButtonEntity
from homeassistant.components.switch import SwitchEntity
from homeassistant.components.binary_sensor import BinarySensorEntity
from homeassistant.components.sensor import SensorEntity
Expand Down Expand Up @@ -70,6 +71,7 @@ def __init__(
name: str
) -> None:
self._device = device
self._hash = device.md5
self._id = id
self._name = name
self._is_new = True
Expand Down Expand Up @@ -116,7 +118,10 @@ async def async_added_to_hass(self) -> None:

@callback
def _schedule_immediate_update(self) -> None:
self.async_schedule_update_ha_state(True)
_hash = self._device.md5
if _hash != self._hash:
self._hash = _hash
self.async_schedule_update_ha_state(True)

async def will_remove_from_hass(self) -> None:
if self.unsub_update:
Expand Down Expand Up @@ -289,16 +294,12 @@ async def async_turn_off(self, **kwargs) -> None:

await self.async_force_update()

class Scene(LedFxSwitch):
class Scene(ButtonEntity, LedFxEntity):
@property
def icon(self) -> Optional[str]:
return "mdi:image"

@property
def is_on(self) -> bool:
return False

async def async_turn_on(self, **kwargs) -> None:
async def async_press(self) -> None:
if not self.available:
return

Expand Down Expand Up @@ -386,4 +387,4 @@ async def async_select_option(self, value: str) -> None:
try:
await self._device.api.set_audio_device(self.options.index(value) + 1)
except Exception as e:
_LOGGER.error("ERROR LedFx send command %r", e)
_LOGGER.error("ERROR LedFx send command %r", e)
18 changes: 18 additions & 0 deletions custom_components/ledfx/core/device.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import logging
import hashlib
import json

from typing import Optional

Expand Down Expand Up @@ -93,6 +95,22 @@ def url(self) -> str:
def entities(self) -> dict:
return self._entities

@property
def md5(self) -> str:
return hashlib.md5(
json.dumps({
"id": self._id,
"name": self._name,
"version": self._version,
"model": self._model,
"effects": dict(self._effects) if self._effects is not None else None,
"is_available": self._is_available,
"entities_data": self._entities_data,
"effect_properties": self._effect_properties,
"last_effect": dict(self._last_effect) if self._last_effect is not None else None
}).encode('utf-8')
).hexdigest()

async def async_update_available(self, is_available: bool) -> None:
self._is_available = is_available

Expand Down
36 changes: 33 additions & 3 deletions custom_components/ledfx/core/effects.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,36 @@
from typing import Optional
import logging

class Effect(object):
from typing import Optional
import datetime
import decimal

_LOGGER = logging.getLogger(__name__)

class Jsonable(object):
def __iter__(self):
for attr, value in self.__dict__.items():
if isinstance(value, datetime.datetime):
iso = value.isoformat()
yield attr, iso
elif isinstance(value, decimal.Decimal):
yield attr, str(value)
elif hasattr(value, '__iter__'):
if hasattr(value, 'pop'):
a = []
for subval in value:
if hasattr(subval, '__iter__') and not isinstance(subval, str):
a.append(dict(subval))
else:
a.append(subval)
yield attr, a
elif isinstance(value, str):
yield attr, value
else:
yield attr, dict(value)
else:
yield attr, value

class Effect(Jsonable):
def __init__(
self,
code: str,
Expand Down Expand Up @@ -62,7 +92,7 @@ async def async_brightness_enable(self) -> None:
async def async_append_entity(self, entity: str) -> None:
self._support_entities.append(entity)

class Effects(object):
class Effects(Jsonable):
def __init__(self) -> None:
self._effects = {}

Expand Down
2 changes: 1 addition & 1 deletion custom_components/ledfx/core/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ async def async_setup(self) -> bool:
self.set_scan_interval()
self.config_entry.add_update_listener(self.async_options_updated)

for domain in ['light', 'sensor', 'binary_sensor', 'number', 'select', 'switch']:
for domain in ['light', 'sensor', 'binary_sensor', 'number', 'select', 'button', 'switch']:
self.hass.async_create_task(
self.hass.config_entries.async_forward_entry_setup(self.config_entry, domain)
)
Expand Down
2 changes: 1 addition & 1 deletion custom_components/ledfx/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"domain": "ledfx",
"name": "LedFx",
"version": "1.2.1",
"version": "1.3.0",
"documentation": "https://github.com/dmamontov/hass-ledfx",
"issue_tracker": "https://github.com/dmamontov/hass-ledfx/issues",
"config_flow": true,
Expand Down

0 comments on commit 141d0e9

Please sign in to comment.