Trying to refactor the tracker entity_status

This commit is contained in:
2021-04-10 22:47:32 +02:00
parent ec63a36c81
commit 093bebc391
5 changed files with 107 additions and 110 deletions

View File

@@ -2,10 +2,19 @@
import logging
from datetime import timedelta
from typing import Any, Mapping
from homeassistant.core import callback
from homeassistant.components.switch import SwitchEntity
from homeassistant.components.switch import ENTITY_ID_FORMAT
from homeassistant.helpers.update_coordinator import (
CoordinatorEntity,
DataUpdateCoordinator,
)
import georideapilib.api as GeoRideApi
from .const import DOMAIN as GEORIDE_DOMAIN
@@ -16,19 +25,21 @@ _LOGGER = logging.getLogger(__name__)
async def async_setup_entry(hass, config_entry, async_add_entities): # pylint: disable=W0613
"""Set up GeoRide tracker based off an entry."""
georide_context = hass.data[GEORIDE_DOMAIN]["context"]
token = await georide_context.get_token()
if token is None:
return False
_LOGGER.info('Current georide token: %s', token)
trackers = await hass.async_add_executor_job(GeoRideApi.get_trackers, token)
georide_context = hass.data[GEORIDE_DOMAIN]["context"]
trackers = georide_context.get_trackers()
update_interval = timedelta(seconds=1)
lock_switch_entities = []
for tracker in trackers:
entity = GeoRideLockSwitchEntity(hass, tracker.tracker_id, georide_context.get_token,
georide_context.get_tracker, data=tracker)
coordinator = DataUpdateCoordinator[Mapping[str, Any]](
hass,
_LOGGER,
name=tracker.tracker_name,
update_method=georide_context.refresh_trackers,
update_interval=update_interval
)
entity = GeoRideLockSwitchEntity(coordinator, tracker, hass)
hass.data[GEORIDE_DOMAIN]["devices"][tracker.tracker_id] = entity
lock_switch_entities.append(entity)
@@ -38,65 +49,61 @@ async def async_setup_entry(hass, config_entry, async_add_entities): # pylint: d
class GeoRideLockSwitchEntity(SwitchEntity):
class GeoRideLockSwitchEntity(CoordinatorEntity, SwitchEntity):
"""Represent a tracked device."""
def __init__(self, hass, tracker_id, get_token_callback, get_tracker_callback, data):
def __init__(self, coordinator: DataUpdateCoordinator[Mapping[str, Any]], tracker, hass):
"""Set up GeoRide entity."""
self._tracker_id = tracker_id
self._data = data or {}
self._get_token_callback = get_token_callback
self._get_tracker_callback = get_tracker_callback
self._name = data.tracker_name
self._is_on = data.is_locked
self.entity_id = ENTITY_ID_FORMAT.format("lock") +"." + str(tracker_id)
self._state = {}
super().__init__(coordinator)
self._tracker = tracker
self._name = tracker.tracker_name
self._is_on = tracker.is_locked
self.entity_id = ENTITY_ID_FORMAT.format("lock") +"." + str(tracker.tracker_id)
self._hass = hass
async def async_turn_on(self, **kwargs):
""" lock the GeoRide tracker """
_LOGGER.info('async_turn_on %s', kwargs)
token = await self._get_token_callback()
georide_context = self._hass.data[GEORIDE_DOMAIN]["context"]
token = await georide_context.get_token()
success = await self._hass.async_add_executor_job(GeoRideApi.lock_tracker,
token, self._tracker_id)
token, self._tracker_id)
if success:
self._data.is_locked = True
self._tracker.is_locked = True
self._is_on = True
async def async_turn_off(self, **kwargs):
""" unlock the GeoRide tracker """
_LOGGER.info('async_turn_off %s', kwargs)
token = await self._get_token_callback()
georide_context = self._hass.data[GEORIDE_DOMAIN]["context"]
token = await georide_context.get_token()
success = await self._hass.async_add_executor_job(GeoRideApi.unlock_tracker,
token, self._tracker_id)
if success:
self._data.is_locked = False
self._tracker.is_locked = False
self._is_on = False
async def async_toggle(self, **kwargs):
""" toggle lock the georide tracker """
_LOGGER.info('async_toggle %s', kwargs)
token = await self._get_token_callback()
georide_context = self._hass.data[GEORIDE_DOMAIN]["context"]
token = await georide_context.get_token()
result = await self._hass.async_add_executor_job(GeoRideApi.toogle_lock_tracker,
token, self._tracker_id)
self._data.is_locked = result
self._tracker.is_locked = result
self._is_on = result
async def async_update(self):
""" update the current tracker"""
_LOGGER.info('update %s', self._tracker_id)
self._data = await self._get_tracker_callback(self._tracker_id)
_LOGGER.debug('data %s', self._data.is_locked)
self._name = self._data.tracker_name
self._is_on = self._data.is_locked
_LOGGER.debug('update')
self._name = self._tracker.tracker_name
self._is_on = self._tracker.is_lockedtracker
@property
def unique_id(self):
"""Return the unique ID."""
return self._tracker_id
return self._tracker.tracker_id
@property
def name(self):
@@ -107,16 +114,6 @@ class GeoRideLockSwitchEntity(SwitchEntity):
def is_on(self):
""" GeoRide switch status """
return self._is_on
@property
def get_token_callback(self):
""" GeoRide switch token callback method """
return self._get_token_callback
@property
def get_tracker_callback(self):
""" GeoRide switch token callback method """
return self._get_tracker_callback
@property
def icon(self):
@@ -131,7 +128,7 @@ class GeoRideLockSwitchEntity(SwitchEntity):
"""Return the device info."""
return {
"name": self.name,
"identifiers": {(GEORIDE_DOMAIN, self._tracker_id)},
"identifiers": {(GEORIDE_DOMAIN, self._tracker.tracker_id)},
"manufacturer": "GeoRide"
}