Skip to content

Commit

Permalink
refactor entity_category and switch
Browse files Browse the repository at this point in the history
  • Loading branch information
krahabb committed Jan 21, 2023
1 parent fdf399b commit 8656990
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 104 deletions.
11 changes: 8 additions & 3 deletions custom_components/meross_lan/cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,13 @@
from .helpers import LOGGER, get_entity_last_state, versiontuple

from .merossclient import const as mc
from .meross_entity import _MerossEntity, platform_setup_entry, platform_unload_entry
from .meross_entity import (
_MerossEntity,
platform_setup_entry, platform_unload_entry,
EntityCategory,
)
from .number import MLConfigNumber
from .switch import MLConfigSwitch
from .switch import MLSwitch
from .const import (
PARAM_GARAGEDOOR_TRANSITION_MAXDURATION,
PARAM_GARAGEDOOR_TRANSITION_MINDURATION,
Expand Down Expand Up @@ -338,8 +342,9 @@ def _ack_callback():
)


class MLGarageConfigSwitch(MLConfigSwitch):
class MLGarageConfigSwitch(MLSwitch):

_attr_entity_category = EntityCategory.CONFIG

def __init__(self, device, key: str):
self._key = key
Expand Down
9 changes: 3 additions & 6 deletions custom_components/meross_lan/devices/mts100.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
EVENT_START, EVENT_END, EVENT_SUMMARY,
EVENT_UID, EVENT_RECURRENCE_ID, EVENT_RRULE,
)
from ..meross_entity import ENTITY_CATEGORY_CONFIG
from ..meross_entity import EntityCategory
from ..merossclient import const as mc # mEROSS cONST
from ..helpers import clamp

Expand Down Expand Up @@ -208,6 +208,8 @@ def get_event(self) -> CalendarEvent:

class Mts100Schedule(MLCalendar):

_attr_entity_category = EntityCategory.CONFIG


def __init__(self, climate: Mts100Climate):
super().__init__(climate.device, climate.id, mc.KEY_SCHEDULE, None, climate.subdevice)
Expand Down Expand Up @@ -340,11 +342,6 @@ def supported_features(self) -> int:
return CalendarEntityFeature.CREATE_EVENT | CalendarEntityFeature.DELETE_EVENT


@property
def entity_category(self) -> int:
return ENTITY_CATEGORY_CONFIG


@property
def event(self) -> CalendarEvent | None:
"""Return the next upcoming event."""
Expand Down
22 changes: 2 additions & 20 deletions custom_components/meross_lan/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
_MerossToggle,
platform_setup_entry, platform_unload_entry,
STATE_ON, STATE_OFF,
ENTITY_CATEGORY_CONFIG,
EntityCategory,
)
from .const import DND_ID

Expand Down Expand Up @@ -226,19 +226,6 @@ def __init__(
device, channel, None, None, None,
mc.NS_APPLIANCE_CONTROL_TOGGLEX if self._hastogglex else None)


"""
self._light = {
#"onoff": 0,
"capacity": CAPACITY_LUMINANCE,
"channel": channel,
#"rgb": 16753920,
#"temperature": 100,
"luminance": 100,
"transform": 0,
"gradual": 0
}
"""
self._light = dict()
"""
capacity is set in abilities when using mc.NS_APPLIANCE_CONTROL_LIGHT
Expand Down Expand Up @@ -410,7 +397,7 @@ class MLDNDLightEntity(_MerossToggle, LightEntity):
PLATFORM = PLATFORM_LIGHT

_attr_supported_color_modes = { COLOR_MODE_ONOFF }

_attr_entity_category = EntityCategory.CONFIG

def __init__(self, device: MerossDevice):
super().__init__(device, None, DND_ID, mc.KEY_DNDMODE, None, None)
Expand All @@ -426,11 +413,6 @@ def color_mode(self):
return COLOR_MODE_ONOFF


@property
def entity_category(self):
return ENTITY_CATEGORY_CONFIG


async def async_turn_on(self, **kwargs) -> None:
def _ack_callback():
self.update_state(STATE_ON)
Expand Down
12 changes: 10 additions & 2 deletions custom_components/meross_lan/meross_device_hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from .climate import PLATFORM_CLIMATE
from .binary_sensor import PLATFORM_BINARY_SENSOR, MLBinarySensor, DEVICE_CLASS_WINDOW
from .number import PLATFORM_NUMBER, MLHubAdjustNumber
from .switch import PLATFORM_SWITCH, MLHubSwitch
from .switch import PLATFORM_SWITCH, MLSwitch, DEVICE_CLASS_SWITCH
from .calendar import PLATFORM_CALENDAR
from .helpers import LOGGER
from .const import (
Expand Down Expand Up @@ -373,7 +373,15 @@ def _parse_adjust(self, p_adjust: dict) -> None:

def _parse_togglex(self, p_togglex: dict) -> None:
if self.switch_togglex is None:
self.switch_togglex = MLHubSwitch(self)
self.switch_togglex = MLSwitch(
self.hub,
self.id, None,
DEVICE_CLASS_SWITCH,
self,
mc.NS_APPLIANCE_HUB_TOGGLEX
)
self.switch_togglex.key_channel = mc.KEY_ID

self.switch_togglex._parse_togglex(p_togglex)


Expand Down
28 changes: 19 additions & 9 deletions custom_components/meross_lan/meross_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@
)
try:# 2022.2 new symbols
from homeassistant.helpers.entity import EntityCategory
ENTITY_CATEGORY_CONFIG = EntityCategory.CONFIG
ENTITY_CATEGORY_DIAGNOSTIC = EntityCategory.DIAGNOSTIC
except:
ENTITY_CATEGORY_CONFIG = 'config'
ENTITY_CATEGORY_DIAGNOSTIC = 'diagnostic'

class EntityCategory:
CONFIG = "config"
DIAGNOSTIC = "diagnostic"

from .merossclient import const as mc, get_namespacekey, get_productnameuuid
from .helpers import LOGGER
Expand All @@ -47,6 +45,7 @@ class _MerossEntity:
_attr_state: StateType = None
_attr_device_class: str | None
_attr_name: str | None = None
_attr_entity_category: EntityCategory | None = None

def __init__(
self,
Expand Down Expand Up @@ -149,6 +148,11 @@ def device_class(self) -> str | None:
return self._attr_device_class


@property
def entity_category(self) -> EntityCategory | None:
return self._attr_entity_category


@property
def should_poll(self) -> bool:
return False
Expand Down Expand Up @@ -210,6 +214,12 @@ def update_onoff(self, onoff) -> None:

class _MerossToggle(_MerossEntity):

# customize the request payload for differen
# devices api. see 'request_onoff' to see how
namespace: str
key_namespace: str
key_channel: str = mc.KEY_CHANNEL
key_onoff: str = mc.KEY_ONOFF

def __init__(
self,
Expand All @@ -222,7 +232,7 @@ def __init__(
):
super().__init__(device, channel, entitykey, device_class, subdevice)
self.namespace = namespace
self.key = None if namespace is None else get_namespacekey(namespace)
self.key_namespace = None if namespace is None else get_namespacekey(namespace)


async def async_turn_on(self, **kwargs) -> None:
Expand All @@ -243,16 +253,16 @@ def _ack_callback():
self.device.request(
self.namespace,
mc.METHOD_SET,
{self.key: {mc.KEY_CHANNEL: self.channel, mc.KEY_ONOFF: onoff}},
{self.key_namespace: {self.key_channel: self.channel, self.key_onoff: onoff}},
_ack_callback)


def _parse_toggle(self, payload: dict):
self.update_onoff(payload.get(mc.KEY_ONOFF))
self.update_onoff(payload.get(self.key_onoff))


def _parse_togglex(self, payload: dict):
self.update_onoff(payload.get(mc.KEY_ONOFF))
self.update_onoff(payload.get(self.key_onoff))


"""
Expand Down
14 changes: 5 additions & 9 deletions custom_components/meross_lan/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from .meross_entity import (
_MerossEntity,
platform_setup_entry, platform_unload_entry,
ENTITY_CATEGORY_CONFIG,
EntityCategory,
)
from .sensor import CLASS_TO_UNIT_MAP
from .helpers import LOGGER
Expand All @@ -43,6 +43,8 @@ class MLConfigNumber(_MerossEntity, NumberEntity):

PLATFORM = PLATFORM_NUMBER

_attr_entity_category = EntityCategory.CONFIG

multiplier = 1

_attr_mode = NUMBERMODE_BOX
Expand All @@ -52,10 +54,6 @@ class MLConfigNumber(_MerossEntity, NumberEntity):
_attr_native_unit_of_measurement: str | None


@property
def entity_category(self):
return ENTITY_CATEGORY_CONFIG

@property
def native_max_value(self):
return self._attr_native_max_value
Expand Down Expand Up @@ -87,6 +85,8 @@ class MLConfigNumber(_MerossEntity, NumberEntity):

PLATFORM = PLATFORM_NUMBER

_attr_entity_category = EntityCategory.CONFIG

multiplier = 1

_attr_mode = NUMBERMODE_BOX
Expand All @@ -96,10 +96,6 @@ class MLConfigNumber(_MerossEntity, NumberEntity):
_attr_native_unit_of_measurement: str | None


@property
def entity_category(self):
return ENTITY_CATEGORY_CONFIG

@property
def native_max_value(self):
return self._attr_native_max_value
Expand Down
4 changes: 2 additions & 2 deletions custom_components/meross_lan/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
from .meross_entity import (
_MerossEntity,
platform_setup_entry, platform_unload_entry,
ENTITY_CATEGORY_CONFIG, ENTITY_CATEGORY_DIAGNOSTIC,
EntityCategory,
)
from .const import (
PARAM_ENERGY_UPDATE_PERIOD, PARAM_SIGNAL_UPDATE_PERIOD,
Expand Down Expand Up @@ -271,7 +271,7 @@ def __init__(self, api, descriptor: MerossDeviceDescriptor, entry) -> None:
# so we drop the device_class (none) but we let the 'entitykey' parameter
# to keep the same value so the entity id inside HA remains stable (#239)
self._sensor_runtime = MLSensor(self, None, DEVICE_CLASS_SIGNAL_STRENGTH, None, None)
self._sensor_runtime._attr_entity_category = ENTITY_CATEGORY_DIAGNOSTIC
self._sensor_runtime._attr_entity_category = EntityCategory.DIAGNOSTIC
self._sensor_runtime._attr_native_unit_of_measurement = PERCENTAGE
self._sensor_runtime._attr_icon = 'mdi:wifi'

Expand Down
54 changes: 1 addition & 53 deletions custom_components/meross_lan/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
_MerossToggle,
platform_setup_entry, platform_unload_entry,
STATE_OFF, STATE_ON,
ENTITY_CATEGORY_CONFIG,
)


Expand All @@ -36,51 +35,12 @@ class MLSwitch(_MerossToggle, SwitchEntity):
PLATFORM = PLATFORM_SWITCH


"""def __init__(
self,
device: 'MerossDevice',
channel: object,
entitykey: str,
device_class: str,
subdevice: 'MerossSubDevice',
namespace: str
):
super().__init__(device, channel, entitykey, device_class, namespace)
#def __init__(self, device: 'MerossDevice', channel: object, namespace: str):
# super().__init__(device, channel, None, DEVICE_CLASS_OUTLET, namespace)
"""

@staticmethod
def build_for_device(device: "MerossDevice", channel: object, namespace: str):
return MLSwitch(device, channel, None, DEVICE_CLASS_OUTLET, None, namespace)



class MLHubSwitch(MLSwitch):

def __init__(
self,
subdevice: 'MerossSubDevice'
):
super().__init__(subdevice.hub, subdevice.id, None, DEVICE_CLASS_SWITCH, subdevice, mc.NS_APPLIANCE_HUB_TOGGLEX)


def request_onoff(self, onoff):
# this is the meross executor code
# override for switches not implemented
# by a toggle like api
def _ack_callback():
self.update_onoff(onoff)

self.device.request(
mc.NS_APPLIANCE_HUB_TOGGLEX,
mc.METHOD_SET,
{mc.KEY_TOGGLEX: [{mc.KEY_ID: self.channel, mc.KEY_ONOFF: onoff}]},
_ack_callback)



class ToggleXMixin:

def __init__(self, api, descriptor, entry) -> None:
Expand Down Expand Up @@ -158,16 +118,4 @@ def _parse_toggle(self, payload: dict):
"""
if isinstance(payload, dict):
entity: MLSwitch = self.entities[payload.get(mc.KEY_CHANNEL, 0)]
entity._parse_toggle(payload)



class MLConfigSwitch(MLSwitch):
"""
configuration switch
"""
PLATFORM = PLATFORM_SWITCH

@property
def entity_category(self):
return ENTITY_CATEGORY_CONFIG
entity._parse_toggle(payload)

0 comments on commit 8656990

Please sign in to comment.