Skip to content

Commit

Permalink
Fix NumberEntity backward compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed Jan 4, 2024
1 parent 80505b8 commit e4c75fc
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 26 deletions.
3 changes: 1 addition & 2 deletions custom_components/sonoff/core/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,8 @@ def spec(cls, base: str = None, enabled: bool = None, **kwargs) -> type:
LED = spec(XToggle, param="sledOnline", uid="led", enabled=False)
RSSI = spec(XSensor, param="rssi", enabled=False)
PULSE = spec(XToggle, param="pulse", enabled=False)
PULSEWIDTH = spec(XPulseWidth, param="pulseWidth", enabled=False)

SPEC_SWITCH = [XSwitch, LED, RSSI, PULSE, PULSEWIDTH]
SPEC_SWITCH = [XSwitch, LED, RSSI, PULSE, XPulseWidth]
SPEC_1CH = [Switch1, LED, RSSI]
SPEC_2CH = [Switch1, Switch2, LED, RSSI]
SPEC_3CH = [Switch1, Switch2, Switch3, LED, RSSI]
Expand Down
2 changes: 0 additions & 2 deletions custom_components/sonoff/core/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ class XEntity(Entity):
param: str = None
uid: str = None

# fix Hass v2021.12 empty attribute bug
_attr_is_on = None
_attr_should_poll = False

def __init__(self, ewelink: XRegistry, device: XDevice) -> None:
Expand Down
25 changes: 3 additions & 22 deletions custom_components/sonoff/number.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
from homeassistant.components.number import NumberEntity
from homeassistant.const import MAJOR_VERSION, MINOR_VERSION

from .core.const import DOMAIN
from .core.entity import XEntity
from .core.ewelink import SIGNAL_ADD_ENTITIES, XRegistry

PARALLEL_UPDATES = 0 # fix entity_platform parallel_updates Semaphore

# https://github.com/home-assistant/core/blob/2022.7.0/homeassistant/components/number/__init__.py
BACKWARD = {
"_attr_max_value": "_attr_native_max_value",
"_attr_min_value": "_attr_native_min_value",
"_attr_step": "_attr_native_step",
"_attr_value": "_attr_native_value",
"async_set_value": "async_set_native_value",
}


async def async_setup_entry(hass, config_entry, add_entities):
ewelink: XRegistry = hass.data[DOMAIN][config_entry.entry_id]
Expand All @@ -27,10 +17,6 @@ async def async_setup_entry(hass, config_entry, add_entities):

# noinspection PyAbstractClass
class XNumber(XEntity, NumberEntity):
"""
customizable number entity for simple 'params'
"""

multiply: float = None
round: int = None

Expand All @@ -48,17 +34,12 @@ async def async_set_native_value(self, value: float) -> None:
value /= self.multiply
await self.ewelink.send(self.device, {self.param: int(value)})

# backward compatibility fix
if (MAJOR_VERSION, MINOR_VERSION) < (2022, 7):
# fix Hass v2021.12 empty attribute bug
_attr_native_value = None

def __getattribute__(self, name: str):
name = BACKWARD.get(name, name)
return super().__getattribute__(name)
class XPulseWidth(XNumber):
param = "pulseWidth"

__attr_entity_registry_enabled_default = False

class XPulseWidth(XNumber):
_attr_native_max_value = 36000
_attr_native_min_value = 0.5
_attr_native_step = 0.5
Expand Down
54 changes: 54 additions & 0 deletions tests/test_backward.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from homeassistant.components.button import ButtonEntity
from homeassistant.components.climate.const import ClimateEntityFeature, HVACMode
from homeassistant.components.fan import FanEntityFeature, FanEntity
from homeassistant.components.light import ColorMode, LightEntityFeature
from homeassistant.components.sensor import (
SensorEntity,
SensorDeviceClass,
SensorStateClass,
)
from homeassistant.const import (
EntityCategory,
UnitOfElectricCurrent,
UnitOfElectricPotential,
UnitOfEnergy,
UnitOfPower,
UnitOfTemperature,
)


def test_2021_9_0():
sensor = SensorEntity()
assert sensor.native_value is None
assert sensor.native_unit_of_measurement is None


def test_2021_12_0():
assert ButtonEntity
assert EntityCategory
assert FanEntity().percentage is 0
assert SensorDeviceClass
assert SensorStateClass


def test_2022_5_0():
assert ClimateEntityFeature
assert HVACMode
assert FanEntityFeature
assert ColorMode
assert LightEntityFeature


def test_2022_11_0():
assert UnitOfEnergy
assert UnitOfPower
assert UnitOfTemperature


def test_2023_1_0():
assert UnitOfElectricCurrent
assert UnitOfElectricPotential


def test_2024_1_cached_properties():
pass

0 comments on commit e4c75fc

Please sign in to comment.