Add hass object on entity items

develop
Matthieu DUVAL 4 years ago
parent af1df1cd84
commit bcaa4bf957

@ -209,7 +209,7 @@ class GeoRideContext:
async def refresh_trackers(self):
"""Used to refresh the tracker list"""
_LOGGER.info("Tracker list refresh")
self._georide_trackers = await GeoRideApi.get_trackers(await self.get_token())
self._georide_trackers = await self._hass.async_add_executor_job(GeoRideApi.get_trackers, await self.get_token)
@property
def socket(self):

@ -23,11 +23,15 @@ async def async_setup_entry(hass, config_entry, async_add_entities): # pylint: d
binary_sensor_entities = []
for tracker in trackers:
stolen_entity = GeoRideStolenBinarySensorEntity(tracker.tracker_id, georide_context.get_token,
georide_context.get_tracker, data=tracker)
stolen_entity = GeoRideStolenBinarySensorEntity(hass, tracker.tracker_id,
georide_context.get_token,
georide_context.get_tracker,
data=tracker)
hass.data[GEORIDE_DOMAIN]["devices"][tracker.tracker_id] = stolen_entity
crashed_entity = GeoRideCrashedBinarySensorEntity(tracker.tracker_id, georide_context.get_token,
georide_context.get_tracker, data=tracker)
crashed_entity = GeoRideCrashedBinarySensorEntity(hass, tracker.tracker_id,
georide_context.get_token,
georide_context.get_tracker,
data=tracker)
hass.data[GEORIDE_DOMAIN]["devices"][tracker.tracker_id] = crashed_entity
binary_sensor_entities.append(stolen_entity)
binary_sensor_entities.append(crashed_entity)
@ -38,7 +42,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities): # pylint: d
class GeoRideStolenBinarySensorEntity(BinarySensorEntity):
"""Represent a tracked device."""
def __init__(self, tracker_id, get_token_callback, get_tracker_callback, data):
def __init__(self, hass, tracker_id, get_token_callback, get_tracker_callback, data):
"""Set up Georide entity."""
self._tracker_id = tracker_id
self._data = data or {}
@ -48,6 +52,7 @@ class GeoRideStolenBinarySensorEntity(BinarySensorEntity):
self.entity_id = ENTITY_ID_FORMAT.format("is_stolen") + "." + str(tracker_id)
self._state = 0
self._hass = hass
async def async_update(self):
@ -95,7 +100,7 @@ class GeoRideStolenBinarySensorEntity(BinarySensorEntity):
class GeoRideCrashedBinarySensorEntity(BinarySensorEntity):
"""Represent a tracked device."""
def __init__(self, tracker_id, get_token_callback, get_tracker_callback, data):
def __init__(self, hass, tracker_id, get_token_callback, get_tracker_callback, data):
"""Set up Georide entity."""
self._tracker_id = tracker_id
self._data = data or {}
@ -105,6 +110,7 @@ class GeoRideCrashedBinarySensorEntity(BinarySensorEntity):
self.entity_id = ENTITY_ID_FORMAT.format("is_crashed") + "." + str(tracker_id)
self._state = 0
self._hass = hass
async def async_update(self):

@ -28,7 +28,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities): # pylint: d
tracker_entities = []
for tracker in trackers:
entity = GeoRideTrackerEntity(tracker.tracker_id, georide_context.get_token,
entity = GeoRideTrackerEntity(hass, tracker.tracker_id, georide_context.get_token,
georide_context.get_tracker, tracker)
@ -43,7 +43,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities): # pylint: d
class GeoRideTrackerEntity(TrackerEntity):
"""Represent a tracked device."""
def __init__(self, tracker_id, get_token_callback, get_tracker_callback, tracker):
def __init__(self, hass, tracker_id, get_token_callback, get_tracker_callback, tracker):
"""Set up GeoRide entity."""
self._tracker_id = tracker_id
self._get_token_callback = get_token_callback
@ -51,6 +51,7 @@ class GeoRideTrackerEntity(TrackerEntity):
self._name = tracker.tracker_name
self._data = tracker or {}
self.entity_id = DOMAIN + ".{}".format(tracker_id)
self._hass = hass
@property
def unique_id(self):

@ -25,7 +25,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities): # pylint: d
odometer_switch_entities = []
for tracker in trackers:
entity = GeoRideOdometerSensorEntity(tracker.tracker_id, georide_context.get_token,
entity = GeoRideOdometerSensorEntity(hass, tracker.tracker_id, georide_context.get_token,
georide_context.get_tracker, data=tracker)
hass.data[GEORIDE_DOMAIN]["devices"][tracker.tracker_id] = entity
odometer_switch_entities.append(entity)
@ -37,7 +37,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities): # pylint: d
class GeoRideOdometerSensorEntity(SwitchEntity):
"""Represent a tracked device."""
def __init__(self, tracker_id, get_token_callback, get_tracker_callback, data):
def __init__(self, hass, tracker_id, get_token_callback, get_tracker_callback, data):
"""Set up Georide entity."""
self._tracker_id = tracker_id
self._data = data or {}
@ -48,6 +48,7 @@ class GeoRideOdometerSensorEntity(SwitchEntity):
self.entity_id = ENTITY_ID_FORMAT.format("odometer") + "." + str(tracker_id)
self._state = 0
self._hass = hass
async def async_update(self):

@ -27,7 +27,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities): # pylint: d
lock_switch_entities = []
for tracker in trackers:
entity = GeoRideLockSwitchEntity(tracker.tracker_id, georide_context.get_token,
entity = GeoRideLockSwitchEntity(hass, tracker.tracker_id, georide_context.get_token,
georide_context.get_tracker, data=tracker)
hass.data[GEORIDE_DOMAIN]["devices"][tracker.tracker_id] = entity
lock_switch_entities.append(entity)
@ -41,7 +41,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities): # pylint: d
class GeoRideLockSwitchEntity(SwitchEntity):
"""Represent a tracked device."""
def __init__(self, tracker_id, get_token_callback, get_tracker_callback, data):
def __init__(self, hass, tracker_id, get_token_callback, get_tracker_callback, data):
"""Set up GeoRide entity."""
self._tracker_id = tracker_id
self._data = data or {}
@ -51,20 +51,25 @@ class GeoRideLockSwitchEntity(SwitchEntity):
self._is_on = data.is_locked
self.entity_id = ENTITY_ID_FORMAT.format("lock") +"." + str(tracker_id)
self._state = {}
self._hass = hass
def turn_on(self, **kwargs):
async def async_turn_on(self, **kwargs):
""" lock the GeoRide tracker """
_LOGGER.info('async_turn_on %s', kwargs)
success = GeoRideApi.lock_tracker(await self._get_token_callback(), self._tracker_id)
token = await self._get_token_callback()
success = await self._hass.async_add_executor_job(GeoRideApi.lock_tracker,
token, self._tracker_id)
if success:
self._data.is_locked = True
self._is_on = True
def turn_off(self, **kwargs):
async def async_turn_off(self, **kwargs):
""" unlock the GeoRide tracker """
_LOGGER.info('async_turn_off %s', kwargs)
success = GeoRideApi.unlock_tracker(await self._get_token_callback(), self._tracker_id)
token = await self._get_token_callback()
success = await self._hass.async_add_executor_job(GeoRideApi.unlock_tracker,
token, self._tracker_id)
if success:
self._data.is_locked = False
self._is_on = False
@ -72,8 +77,9 @@ class GeoRideLockSwitchEntity(SwitchEntity):
async def async_toggle(self, **kwargs):
""" toggle lock the georide tracker """
_LOGGER.info('async_toggle %s', kwargs)
result = await GeoRideApi.toogle_lock_tracker(await self._get_token_callback(),
self._tracker_id)
token = await self._get_token_callback()
result = await self._hass.async_add_executor_job(GeoRideApi.toogle_lock_tracker,
token, self._tracker_id)
self._data.is_locked = result
self._is_on = result

Loading…
Cancel
Save