Migreate to 0.6.0 and new device architecture

This commit is contained in:
2021-04-11 13:38:15 +02:00
parent 56a8a35479
commit 8a44d22d41
8 changed files with 220 additions and 155 deletions

View File

@@ -3,10 +3,8 @@
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
@@ -18,7 +16,7 @@ from homeassistant.helpers.update_coordinator import (
import georideapilib.api as GeoRideApi
from .const import DOMAIN as GEORIDE_DOMAIN
from .device import Device
_LOGGER = logging.getLogger(__name__)
@@ -30,29 +28,27 @@ async def async_setup_entry(hass, config_entry, async_add_entities): # pylint: d
lock_switch_entities = []
for coordoned_tracker in coordoned_trackers:
tracker = coordoned_tracker['tracker']
tracker_device = coordoned_tracker['tracker_device']
coordinator = coordoned_tracker['coordinator']
entity = GeoRideLockSwitchEntity(coordinator, tracker, hass)
hass.data[GEORIDE_DOMAIN]["devices"][tracker.tracker_id] = coordinator
entity = GeoRideLockSwitchEntity(coordinator, tracker_device, hass)
hass.data[GEORIDE_DOMAIN]["devices"][tracker_device.tracker.tracker_id] = coordinator
lock_switch_entities.append(entity)
async_add_entities(lock_switch_entities)
return True
class GeoRideLockSwitchEntity(CoordinatorEntity, SwitchEntity):
"""Represent a tracked device."""
def __init__(self, coordinator: DataUpdateCoordinator[Mapping[str, Any]], tracker, hass):
def __init__(self, coordinator: DataUpdateCoordinator[Mapping[str, Any]],
tracker_device:Device, hass):
"""Set up GeoRide entity."""
super().__init__(coordinator)
self._tracker = tracker
self._name = tracker.tracker_name
self.entity_id = ENTITY_ID_FORMAT.format("lock") +"." + str(tracker.tracker_id)
self._tracker_device = tracker_device
self._name = tracker_device.tracker.tracker_name
self.entity_id = f"{ENTITY_ID_FORMAT.format('lock')}.{tracker_device.tracker.tracker_id}"# pylint: disable=C0301
self._hass = hass
async def async_turn_on(self, **kwargs):
""" lock the GeoRide tracker """
@@ -60,17 +56,17 @@ class GeoRideLockSwitchEntity(CoordinatorEntity, SwitchEntity):
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.tracker_id)
token, self._tracker_device.tracker.tracker_id)
if success:
self._tracker.is_locked = True
async def async_turn_off(self, **kwargs):
""" unlock the GeoRide tracker """
_LOGGER.info('async_turn_off %s', kwargs)
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.tracker_id)
token, self._tracker_device.tracker.tracker_id)
if success:
self._tracker.is_locked = False
@@ -80,7 +76,7 @@ class GeoRideLockSwitchEntity(CoordinatorEntity, SwitchEntity):
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.tracker_id)
token, self._tracker_device.tracker.tracker_id)
self._tracker.is_locked = result
@property
@@ -90,29 +86,22 @@ class GeoRideLockSwitchEntity(CoordinatorEntity, SwitchEntity):
@property
def name(self):
""" GeoRide switch name """
return self._name
""" GeoRide odometer name """
return f"{self._name} lock"
@property
def is_on(self):
""" GeoRide switch status """
return self._tracker.is_locked
return self._tracker_device.tracker.is_locked
@property
def icon(self):
"""return the entity icon"""
if self._tracker.tracker_id:
if self._tracker_device.tracker.is_locked:
return "mdi:lock"
return "mdi:lock-open"
@property
def device_info(self):
"""Return the device info."""
return {
"name": self.name,
"identifiers": {(GEORIDE_DOMAIN, self._tracker.tracker_id)},
"manufacturer": "GeoRide"
}
return self._tracker_device.device_info()