commit
b84a4cfd7f
@ -0,0 +1,157 @@
|
||||
""" binary_sensor for GeoRide object """
|
||||
|
||||
import logging
|
||||
|
||||
from typing import Any, Mapping
|
||||
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.components.binary_sensor import BinarySensorEntity
|
||||
from homeassistant.components.binary_sensor import ENTITY_ID_FORMAT
|
||||
from homeassistant.helpers.update_coordinator import (
|
||||
CoordinatorEntity,
|
||||
DataUpdateCoordinator
|
||||
)
|
||||
|
||||
|
||||
from .const import DOMAIN as GEORIDE_DOMAIN
|
||||
from .device import Device
|
||||
|
||||
_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"]
|
||||
entities = []
|
||||
coordoned_trackers = georide_context.get_coordoned_trackers()
|
||||
for coordoned_tracker in coordoned_trackers:
|
||||
tracker_device = coordoned_tracker['tracker_device']
|
||||
coordinator = coordoned_tracker['coordinator']
|
||||
|
||||
entities.append(GeoRideStolenBinarySensorEntity(coordinator, tracker_device))
|
||||
entities.append(GeoRideCrashedBinarySensorEntity(coordinator, tracker_device))
|
||||
entities.append(GeoRideOwnerBinarySensorEntity(coordinator, tracker_device))
|
||||
entities.append(GeoRideActiveSubscriptionBinarySensorEntity(coordinator, tracker_device))
|
||||
|
||||
hass.data[GEORIDE_DOMAIN]["devices"][tracker_device.tracker.tracker_id] = coordinator
|
||||
|
||||
async_add_entities(entities, True)
|
||||
|
||||
return True
|
||||
|
||||
|
||||
class GeoRideBinarySensorEntity(CoordinatorEntity, BinarySensorEntity):
|
||||
"""Represent a tracked device."""
|
||||
def __init__(self, coordinator: DataUpdateCoordinator[Mapping[str, Any]],
|
||||
tracker_device: Device):
|
||||
"""Set up Georide entity."""
|
||||
super().__init__(coordinator)
|
||||
self._tracker_device = tracker_device
|
||||
self._name = tracker_device.tracker.tracker_name
|
||||
|
||||
self.entity_id = f"{ENTITY_ID_FORMAT.format('binary_sensor')}.{tracker_device.tracker.tracker_id}"# pylint: disable=C0301
|
||||
self._is_on = False
|
||||
|
||||
@property
|
||||
def device_info(self):
|
||||
"""Return the device info."""
|
||||
return self._tracker_device.device_info
|
||||
|
||||
class GeoRideStolenBinarySensorEntity(GeoRideBinarySensorEntity):
|
||||
"""Represent a tracked device."""
|
||||
def __init__(self, coordinator: DataUpdateCoordinator[Mapping[str, Any]],
|
||||
tracker_device: Device):
|
||||
"""Set up Georide entity."""
|
||||
super().__init__(coordinator, tracker_device)
|
||||
self.entity_id = f"{ENTITY_ID_FORMAT.format('is_stolen')}.{tracker_device.tracker.tracker_id}"# pylint: disable=C0301
|
||||
|
||||
@property
|
||||
def unique_id(self):
|
||||
"""Return the unique ID."""
|
||||
return f"is_stolen_{self._tracker_device.tracker.tracker_id}"
|
||||
|
||||
@property
|
||||
def is_on(self):
|
||||
"""state value property"""
|
||||
return self._tracker_device.tracker.is_stolen
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
""" GeoRide odometer name """
|
||||
return f"{self._name} is stolen"
|
||||
|
||||
|
||||
class GeoRideCrashedBinarySensorEntity(GeoRideBinarySensorEntity):
|
||||
"""Represent a tracked device."""
|
||||
|
||||
def __init__(self, coordinator: DataUpdateCoordinator[Mapping[str, Any]],
|
||||
tracker_device: Device):
|
||||
"""Set up Georide entity."""
|
||||
super().__init__(coordinator, tracker_device)
|
||||
self.entity_id = f"{ENTITY_ID_FORMAT.format('is_crashed')}.{tracker_device.tracker.tracker_id}"# pylint: disable=C0301
|
||||
|
||||
@property
|
||||
def unique_id(self):
|
||||
"""Return the unique ID."""
|
||||
return f"is_crashed_{self._tracker_device.tracker.tracker_id}"
|
||||
|
||||
@property
|
||||
def is_on(self):
|
||||
"""state value property"""
|
||||
return self._tracker_device.tracker.is_crashed
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
""" GeoRide odometer name """
|
||||
return f"{self._name} is crashed"
|
||||
|
||||
class GeoRideActiveSubscriptionBinarySensorEntity(GeoRideBinarySensorEntity):
|
||||
"""Represent a tracked device."""
|
||||
|
||||
def __init__(self, coordinator: DataUpdateCoordinator[Mapping[str, Any]],
|
||||
tracker_device: Device):
|
||||
"""Set up Georide entity."""
|
||||
super().__init__(coordinator, tracker_device)
|
||||
self.entity_id = f"{ENTITY_ID_FORMAT.format('is_active_subscription_')}.{tracker_device.tracker.tracker_id}"# pylint: disable=C0301
|
||||
|
||||
@property
|
||||
def unique_id(self):
|
||||
"""Return the unique ID."""
|
||||
return f"is_active_subscription_{self._tracker_device.tracker.tracker_id}"
|
||||
|
||||
@property
|
||||
def is_on(self):
|
||||
"""state value property"""
|
||||
if self._tracker_device.tracker.subscription_id is not None:
|
||||
return True
|
||||
return False
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
""" GeoRide odometer name """
|
||||
return f"{self._name} has an active subscription"
|
||||
|
||||
class GeoRideOwnerBinarySensorEntity(GeoRideBinarySensorEntity):
|
||||
"""Represent a tracked device."""
|
||||
|
||||
def __init__(self, coordinator: DataUpdateCoordinator[Mapping[str, Any]],
|
||||
tracker_device: Device):
|
||||
"""Set up Georide entity."""
|
||||
super().__init__(coordinator, tracker_device)
|
||||
self.entity_id = f"{ENTITY_ID_FORMAT.format('is_owner')}.{tracker_device.tracker.tracker_id}"# pylint: disable=C0301
|
||||
|
||||
@property
|
||||
def unique_id(self):
|
||||
"""Return the unique ID."""
|
||||
return f"is_owner_{self._tracker_device.tracker.tracker_id}"
|
||||
|
||||
@property
|
||||
def is_on(self):
|
||||
"""state value property"""
|
||||
if self._tracker_device.tracker.role == "owner":
|
||||
return True
|
||||
return False
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
""" GeoRide odometer name """
|
||||
return f"{self._name} is own tracker"
|
||||
|
@ -0,0 +1,57 @@
|
||||
"""Home Assistant representation of an GeoRide Tracker device."""
|
||||
import georideapilib.objects as GeoRideTracker
|
||||
from .const import DOMAIN as GEORIDE_DOMAIN
|
||||
|
||||
|
||||
class Device:
|
||||
"""Home Assistant representation of a GeoRide Tracker device."""
|
||||
|
||||
def __init__(self, tracker):
|
||||
"""Initialize GeoRideTracker device."""
|
||||
self._tracker: GeoRideTracker = tracker
|
||||
|
||||
@property
|
||||
def tracker(self):
|
||||
"""return the tracker"""
|
||||
return self._tracker
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
"""Get the name."""
|
||||
return self._tracker.tracker_name
|
||||
|
||||
@property
|
||||
def manufacturer(self) -> str:
|
||||
"""Get the manufacturer."""
|
||||
return "GeoRide"
|
||||
|
||||
@property
|
||||
def model_name(self) -> str:
|
||||
"""Get the model name."""
|
||||
name = "GeoRide 1"
|
||||
if self._tracker.is_old_tracker:
|
||||
name = "Prototype / GeoRide 1"
|
||||
elif self._tracker.is_second_gen:
|
||||
name = "GeoRide 2 / GeoRide 3"
|
||||
return name
|
||||
|
||||
@property
|
||||
def device_info(self):
|
||||
"""Return the device info."""
|
||||
return {
|
||||
"name": self.name,
|
||||
"identifiers": {(GEORIDE_DOMAIN, self._tracker.tracker_id)},
|
||||
"manufacturer": "GeoRide",
|
||||
"model": self.model_name,
|
||||
"suggested_area": "Garage"
|
||||
}
|
||||
|
||||
|
||||
@property
|
||||
def unique_id(self) -> str:
|
||||
"""Get the unique id."""
|
||||
return {(GEORIDE_DOMAIN, self._tracker.tracker_id)}
|
||||
|
||||
def __str__(self) -> str:
|
||||
"""Get string representation."""
|
||||
return f"GeoRide Device: {self.name}::{self.model_name}::self.unique_id"
|
Loading…
Reference in new issue