Skip to content

Commit

Permalink
Cleanup sensors, use unique_ids
Browse files Browse the repository at this point in the history
  • Loading branch information
sondregronas committed Jul 10, 2024
1 parent 359ca62 commit aa8fed8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 27 deletions.
18 changes: 17 additions & 1 deletion custom_components/ankermake/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def __init__(self, coordinator: AnkerMakeUpdateCoordinator,

self._attr_name = f"{device_info['name']} {description.name}"
self.entity_description = description
self._attr_unique_id = description.key
self._attr_unique_id = f"{device_info['name']}_{description.key}"
self._attr_device_info = device_info

@property
Expand All @@ -133,3 +133,19 @@ def _handle_coordinator_update(self) -> None:

def _update_from_anker(self) -> None:
"""Update the entity. (Used by sensor.py)"""

def _filter_handler(self, key: str):
def td_convert(seconds):
return str(timedelta(seconds=seconds))

if key.startswith('%%TD='):
val = getattr(self.coordinator.ankerdata, key.split('=')[1])
return td_convert(val)
elif key.startswith('='):
return key[1:]
elif key.startswith('%SVC_ONLINE='):
return self.coordinator.ankerdata.get_api_service_online(key.split('=')[1])
elif key.startswith('%SVC_STATE='):
return self.coordinator.ankerdata.get_api_service_status(key.split('=')[1])

return getattr(self.coordinator.ankerdata, key)
8 changes: 0 additions & 8 deletions custom_components/ankermake/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,6 @@ def __init__(self, coordinator, description, dev_info, attrs):
self.attrs = attrs.copy()
self._attr_extra_state_attributes = dict()

def _filter_handler(self, key: str):
if key.startswith('%SVC_ONLINE='):
return self.coordinator.ankerdata.get_api_service_online(key.split('=')[1])
elif key.startswith('%SVC_STATE='):
return self.coordinator.ankerdata.get_api_service_status(key.split('=')[1])

return getattr(self.coordinator.ankerdata, key)

@callback
def _update_from_anker(self) -> None:
try:
Expand Down
19 changes: 1 addition & 18 deletions custom_components/ankermake/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class AnkerMakeSensor(AnkerMakeBaseEntity, SensorEntity):
@callback
def _update_from_anker(self) -> None:
try:
value = getattr(self.coordinator.ankerdata, self.entity_description.key)
value = self._filter_handler(self.entity_description.key)
if self.coordinator.ankerdata.online:
self._attr_available = True
else:
Expand All @@ -39,23 +39,6 @@ def __init__(self, coordinator, description, dev_info, attrs):
self.attrs = attrs.copy()
self._attr_extra_state_attributes = dict()

@staticmethod
def td_convert(seconds):
return str(timedelta(seconds=seconds))

def _filter_handler(self, key: str):
if key.startswith('%%TD='):
val = getattr(self.coordinator.ankerdata, key.split('=')[1])
return self.td_convert(val)
elif key.startswith('='):
return key[1:]
elif key.startswith('%SVC_ONLINE='):
return self.coordinator.ankerdata.get_api_service_online(key.split('=')[1])
elif key.startswith('%SVC_STATE='):
return self.coordinator.ankerdata.get_api_service_status(key.split('=')[1])

return getattr(self.coordinator.ankerdata, key)

@callback
def _update_from_anker(self) -> None:
try:
Expand Down

0 comments on commit aa8fed8

Please sign in to comment.