Skip to content

Commit

Permalink
Use constants instead of literal strings
Browse files Browse the repository at this point in the history
  • Loading branch information
tijsverkoyen committed Jan 23, 2023
1 parent 4139ab1 commit 710a8bd
Show file tree
Hide file tree
Showing 18 changed files with 137 additions and 67 deletions.
4 changes: 2 additions & 2 deletions custom_components/nhc2/entities/alloff_action_active.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def __init__(self, device_instance: CocoAlloffAction, hub, gateway):
self._attr_unique_id = device_instance.uuid + '_alloff_active'
self._attr_should_poll = False

self._attr_state = self._device.status_all_off_active
self._attr_state = self._device.is_all_off_active
self._attr_state_class = None

@property
Expand All @@ -41,7 +41,7 @@ def device_info(self):

@property
def is_on(self) -> bool:
return self._device.status_all_off_active == 'True'
return self._device.is_all_off_active

def on_change(self):
self.schedule_update_ha_state()
4 changes: 2 additions & 2 deletions custom_components/nhc2/entities/alloff_action_basicstate.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def __init__(self, device_instance: CocoAlloffAction, hub, gateway):
self._attr_unique_id = device_instance.uuid + '_alloff_basic_state'
self._attr_should_poll = False

self._attr_state = self._device.status_basic_state
self._attr_state = self._device.is_basic_state_on
self._attr_state_class = None

@property
Expand All @@ -41,7 +41,7 @@ def device_info(self):

@property
def is_on(self) -> bool:
return self._device.status_basic_state == 'On'
return self._device.is_basic_state_on

def on_change(self):
self.schedule_update_ha_state()
4 changes: 2 additions & 2 deletions custom_components/nhc2/entities/dimmer_action_alligned.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def __init__(self, device_instance: CocoDimmerAction, hub, gateway):
self._attr_unique_id = device_instance.uuid + '_aligned'
self._attr_should_poll = False

self._attr_state = self._device.status_aligned
self._attr_state = self._device.is_aligned
self._attr_state_class = None
self._attr_entity_category = EntityCategory.DIAGNOSTIC

Expand All @@ -43,7 +43,7 @@ def device_info(self):

@property
def is_on(self) -> bool:
return self._device.status_aligned
return self._device.is_aligned

def on_change(self):
self.schedule_update_ha_state()
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def state(self) -> float:

def on_change(self):
# Re-enable reporting when it is turned off
if self._device.report_instant_usage is False:
if self._device.is_report_instant_usage is False:
_LOGGER.debug(f'{self.name} re-enabled')
self._device.enable_report_instant_usage(self._gateway)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def __init__(self, device_instance: CocoNasoSmartplug, hub, gateway):
self._attr_unique_id = device_instance.uuid + '_feedback_enabled'
self._attr_should_poll = False

self._attr_state = self._device.feedback_enabled
self._attr_state = self._device.is_feedback_enabled
self._attr_state_class = None
self._attr_entity_category = EntityCategory.DIAGNOSTIC

Expand All @@ -42,8 +42,8 @@ def device_info(self):
}

@property
def is_on(self) -> bool:
return self._device.feedback_enabled
def is_on(self) -> bool | None:
return self._device.is_feedback_enabled

def on_change(self):
self.schedule_update_ha_state()
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def __init__(self, device_instance: CocoNasoSmartplug, hub, gateway):
self._attr_unique_id = device_instance.uuid + '_measuring_only'
self._attr_should_poll = False

self._attr_state = self._device.measuring_only
self._attr_state = self._device.is_measuring_only
self._attr_state_class = None
self._attr_entity_category = EntityCategory.DIAGNOSTIC

Expand All @@ -43,7 +43,7 @@ def device_info(self):

@property
def is_on(self) -> bool:
return self._device.measuring_only
return self._device.is_measuring_only

def on_change(self):
self.schedule_update_ha_state()
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def __init__(self, device_instance: CocoNasoSmartplug, hub, gateway):
self._attr_unique_id = device_instance.uuid + '_report_instant_usage'
self._attr_should_poll = False

self._attr_state = self._device.report_instant_usage
self._attr_state = self._device.is_report_instant_usage
self._attr_state_class = None
self._attr_entity_category = EntityCategory.DIAGNOSTIC

Expand All @@ -43,7 +43,7 @@ def device_info(self):

@property
def is_on(self) -> bool:
return self._device.report_instant_usage
return self._device.is_report_instant_usage

def on_change(self):
self.schedule_update_ha_state()
8 changes: 4 additions & 4 deletions custom_components/nhc2/entities/relay_action_switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,17 @@ def device_info(self):
'via_device': self._hub
}

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

@property
def device_class(self) -> str:
if self._device.model == 'socket':
return SwitchDeviceClass.OUTLET

return SwitchDeviceClass.SWITCH

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

def turn_off(self, **kwargs) -> None:
"""Pass - not in use."""
pass
Expand Down
35 changes: 34 additions & 1 deletion custom_components/nhc2/nhccoco/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,37 @@
MQTT_TOPIC_PUBLIC_RSP = '/system/rsp'
MQTT_TOPIC_SUFFIX_CMD = '/control/devices/cmd'
MQTT_TOPIC_SUFFIX_RSP = '/control/devices/rsp'
MQTT_TOPIC_SUFFIX_EVT = '/control/devices/evt'
MQTT_TOPIC_SUFFIX_EVT = '/control/devices/evt'

DEVICE_DESCRIPTOR_UUID = 'Uuid'
DEVICE_DESCRIPTOR_TYPE = 'Type'
DEVICE_DESCRIPTOR_TECHNOLOGY = 'Technology'
DEVICE_DESCRIPTOR_MODEL = 'Model'
DEVICE_DESCRIPTOR_IDENTIFIER = 'Identifier'
DEVICE_DESCRIPTOR_NAME = 'Name'
DEVICE_DESCRIPTOR_TRAITS = 'Traits'
DEVICE_DESCRIPTOR_PARAMETERS = 'Parameters'
DEVICE_DESCRIPTOR_PROPERTIES = 'Properties'
DEVICE_DESCRIPTOR_ONLINE = 'Online'
DEVICE_DESCRIPTOR_ONLINE_VALUE_TRUE = 'True'

PARAMETER_FEEDBACK_ENABLED = 'FeedbackEnabled'
PARAMETER_FEEDBACK_ENABLED_VALUE_TRUE = 'True'
PARAMETER_MEASURING_ONLY = 'MeasuringOnly'
PARAMETER_MEASURING_ONLY_VALUE_TRUE = 'True'

PROPERTY_ALIGNED = 'Aligned'
PROPERTY_ALIGNED_VALUE_TRUE = 'True'
PROPERTY_ALL_OFF_ACTIVE = 'AllOffActive'
PROPERTY_ALL_OFF_ACTIVE_VALUE_TRUE = 'True'
PROPERTY_BASIC_STATE = 'BasicState'
PROPERTY_BASIC_STATE_VALUE_ON = 'On'
PROPERTY_BASIC_STATE_VALUE_TRIGGERED = 'Triggered'
PROPERTY_BRIGHTNESS = 'Brightness'
PROPERTY_ELECTRICAL_POWER = 'ElectricalPower'
PROPERTY_REPORT_INSTANT_USAGE = 'ReportInstantUsage'
PROPERTY_REPORT_INSTANT_USAGE_VALUE_TRUE = 'True'
PROPERTY_REPORT_INSTANT_USAGE_VALUE_FALSE = 'False'
PROPERTY_STATUS = 'Status'
PROPERTY_STATUS_VALUE_ON = 'On'
PROPERTY_STATUS_VALUE_OFF = 'Off'
26 changes: 20 additions & 6 deletions custom_components/nhc2/nhccoco/devices/alloff_action.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from ..const import DEVICE_DESCRIPTOR_PROPERTIES, PROPERTY_BASIC_STATE, PROPERTY_ALL_OFF_ACTIVE, \
PROPERTY_ALL_OFF_ACTIVE_VALUE_TRUE, PROPERTY_BASIC_STATE_VALUE_ON, PROPERTY_BASIC_STATE_VALUE_TRIGGERED
from .device import CoCoDevice

import logging
Expand All @@ -8,24 +10,36 @@
class CocoAlloffAction(CoCoDevice):
@property
def status_basic_state(self) -> str:
return self.extract_property_value('BasicState')
return self.extract_property_value(PROPERTY_BASIC_STATE)

@property
def status_all_off_active(self) -> str:
return self.extract_property_value('AllOffActive')
return self.extract_property_value(PROPERTY_ALL_OFF_ACTIVE)

@property
def is_on(self) -> bool:
return self.status_basic_state == 'On'
return self.status_basic_state == PROPERTY_BASIC_STATE_VALUE_ON

@property
def is_all_off_active(self) -> bool:
return self.status_all_off_active == PROPERTY_ALL_OFF_ACTIVE_VALUE_TRUE

@property
def is_basic_state_on(self) -> bool:
return self.status_basic_state == PROPERTY_BASIC_STATE_VALUE_ON

def on_change(self, topic: str, payload: dict):
_LOGGER.debug(f'{self.name} changed. Topic: {topic} | Data: {payload}')
if 'Properties' in payload:
self.merge_properties(payload['Properties'])
if DEVICE_DESCRIPTOR_PROPERTIES in payload:
self.merge_properties(payload[DEVICE_DESCRIPTOR_PROPERTIES])

if self._after_change_callbacks:
for callback in self._after_change_callbacks:
callback()

def press(self, gateway):
gateway._add_device_control(self._device.uuid, "BasicState", "Triggered")
gateway._add_device_control(
self._device.uuid,
PROPERTY_BASIC_STATE,
PROPERTY_BASIC_STATE_VALUE_TRIGGERED
)
27 changes: 16 additions & 11 deletions custom_components/nhc2/nhccoco/devices/device.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
from ..const import DEVICE_DESCRIPTOR_UUID, DEVICE_DESCRIPTOR_TYPE, DEVICE_DESCRIPTOR_TECHNOLOGY, \
DEVICE_DESCRIPTOR_MODEL, DEVICE_DESCRIPTOR_IDENTIFIER, DEVICE_DESCRIPTOR_NAME, DEVICE_DESCRIPTOR_TRAITS, \
DEVICE_DESCRIPTOR_PARAMETERS, DEVICE_DESCRIPTOR_PROPERTIES, DEVICE_DESCRIPTOR_ONLINE, \
DEVICE_DESCRIPTOR_ONLINE_VALUE_TRUE

import logging

_LOGGER = logging.getLogger(__name__)


class CoCoDevice():
def __init__(self, json: dict):
self._uuid = json['Uuid']
self._type = json['Type']
self._technology = json['Technology']
self._model = json['Model']
self._identifier = json['Identifier']
self._name = json['Name']
self._traits = json['Traits'] if 'Traits' in json else None
self._parameters = json['Parameters'] if 'Parameters' in json else None
self._properties = json['Properties'] if 'Properties' in json else None
self._uuid = json[DEVICE_DESCRIPTOR_UUID]
self._type = json[DEVICE_DESCRIPTOR_TYPE]
self._technology = json[DEVICE_DESCRIPTOR_TECHNOLOGY]
self._model = json[DEVICE_DESCRIPTOR_MODEL]
self._identifier = json[DEVICE_DESCRIPTOR_IDENTIFIER]
self._name = json[DEVICE_DESCRIPTOR_NAME]
self._traits = json[DEVICE_DESCRIPTOR_TRAITS] if DEVICE_DESCRIPTOR_TRAITS in json else None
self._parameters = json[DEVICE_DESCRIPTOR_PARAMETERS] if DEVICE_DESCRIPTOR_PARAMETERS in json else None
self._properties = json[DEVICE_DESCRIPTOR_PROPERTIES] if DEVICE_DESCRIPTOR_PROPERTIES in json else None

self._after_change_callbacks = []
self._online = json['Online'] if 'Online' in json else None
self._online = json[DEVICE_DESCRIPTOR_ONLINE] if DEVICE_DESCRIPTOR_ONLINE in json else None

@property
def uuid(self) -> str:
Expand Down Expand Up @@ -63,7 +68,7 @@ def is_online(self) -> bool:
"""Device is online"""
if self._online is None:
return None
return self._online == 'True'
return self._online == DEVICE_DESCRIPTOR_ONLINE_VALUE_TRUE

@property
def after_change_callbacks(self):
Expand Down
28 changes: 17 additions & 11 deletions custom_components/nhc2/nhccoco/devices/dimmer_action.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from ..const import DEVICE_DESCRIPTOR_PROPERTIES, PROPERTY_STATUS, PROPERTY_STATUS_VALUE_ON, PROPERTY_STATUS_VALUE_OFF, \
PROPERTY_BRIGHTNESS, PROPERTY_ALIGNED, PROPERTY_ALIGNED_VALUE_TRUE
from .device import CoCoDevice

import logging
Expand All @@ -7,39 +9,43 @@

class CocoDimmerAction(CoCoDevice):
@property
def status_status(self) -> str:
return self.extract_property_value('Status')
def status(self) -> str:
return self.extract_property_value(PROPERTY_STATUS)

@property
def status_brightness(self) -> int:
return int(self.extract_property_value('Brightness'))
return int(self.extract_property_value(PROPERTY_BRIGHTNESS))

@property
def status_aligned(self) -> bool:
return self.extract_property_value('Aligned') == 'True'
def status_aligned(self) -> str:
return self.extract_property_value(PROPERTY_ALIGNED)

@property
def is_on(self) -> bool:
return self.status_status == 'On'
return self.status == PROPERTY_STATUS_VALUE_ON

@property
def is_aligned(self) -> bool:
return self.status_aligned == PROPERTY_ALIGNED_VALUE_TRUE

@property
def support_brightness(self) -> bool:
return True

def on_change(self, topic: str, payload: dict):
_LOGGER.debug(f'{self.name} changed. Topic: {topic} | Data: {payload}')
if 'Properties' in payload:
self.merge_properties(payload['Properties'])
if DEVICE_DESCRIPTOR_PROPERTIES in payload:
self.merge_properties(payload[DEVICE_DESCRIPTOR_PROPERTIES])

if self._after_change_callbacks:
for callback in self._after_change_callbacks:
callback()

def turn_on(self, gateway):
gateway._add_device_control(self.uuid, "Status", "On")
gateway._add_device_control(self.uuid, PROPERTY_STATUS, PROPERTY_STATUS_VALUE_ON)

def turn_off(self, gateway):
gateway._add_device_control(self.uuid, "Status", "Off")
gateway._add_device_control(self.uuid, PROPERTY_STATUS, PROPERTY_STATUS_VALUE_OFF)

def set_brightness(self, gateway, brightness: int):
gateway._add_device_control(self.uuid, "Brightness", str(brightness))
gateway._add_device_control(self.uuid, PROPERTY_BRIGHTNESS, str(brightness))
3 changes: 2 additions & 1 deletion custom_components/nhc2/nhccoco/devices/light_action.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from ..const import PROPERTY_STATUS_VALUE_ON
from .relay_action import CocoRelayAction


class CocoLightAction(CocoRelayAction):
@property
def is_on(self) -> bool:
return self.status_status == 'On'
return self.status == PROPERTY_STATUS_VALUE_ON

@property
def support_brightness(self) -> bool:
Expand Down
Loading

0 comments on commit 710a8bd

Please sign in to comment.