Update myq to use CoordinatorEntity (#39393)
This commit is contained in:
parent
a4f475245c
commit
4e876cb473
2 changed files with 15 additions and 32 deletions
|
@ -14,6 +14,7 @@ from homeassistant.components.binary_sensor import (
|
||||||
DEVICE_CLASS_CONNECTIVITY,
|
DEVICE_CLASS_CONNECTIVITY,
|
||||||
BinarySensorEntity,
|
BinarySensorEntity,
|
||||||
)
|
)
|
||||||
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
|
|
||||||
from .const import DOMAIN, MYQ_COORDINATOR, MYQ_GATEWAY
|
from .const import DOMAIN, MYQ_COORDINATOR, MYQ_GATEWAY
|
||||||
|
|
||||||
|
@ -35,12 +36,12 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||||
async_add_entities(entities, True)
|
async_add_entities(entities, True)
|
||||||
|
|
||||||
|
|
||||||
class MyQBinarySensorEntity(BinarySensorEntity):
|
class MyQBinarySensorEntity(CoordinatorEntity, BinarySensorEntity):
|
||||||
"""Representation of a MyQ gateway."""
|
"""Representation of a MyQ gateway."""
|
||||||
|
|
||||||
def __init__(self, coordinator, device):
|
def __init__(self, coordinator, device):
|
||||||
"""Initialize with API object, device id."""
|
"""Initialize with API object, device id."""
|
||||||
self._coordinator = coordinator
|
super().__init__(coordinator)
|
||||||
self._device = device
|
self._device = device
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
@ -56,7 +57,7 @@ class MyQBinarySensorEntity(BinarySensorEntity):
|
||||||
@property
|
@property
|
||||||
def is_on(self):
|
def is_on(self):
|
||||||
"""Return if the device is online."""
|
"""Return if the device is online."""
|
||||||
if not self._coordinator.last_update_success:
|
if not self.coordinator.last_update_success:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# Not all devices report online so assume True if its missing
|
# Not all devices report online so assume True if its missing
|
||||||
|
@ -64,15 +65,16 @@ class MyQBinarySensorEntity(BinarySensorEntity):
|
||||||
MYQ_DEVICE_STATE_ONLINE, True
|
MYQ_DEVICE_STATE_ONLINE, True
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def available(self) -> bool:
|
||||||
|
"""Entity is always available."""
|
||||||
|
return True
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def unique_id(self):
|
def unique_id(self):
|
||||||
"""Return a unique, Home Assistant friendly identifier for this entity."""
|
"""Return a unique, Home Assistant friendly identifier for this entity."""
|
||||||
return self._device.device_id
|
return self._device.device_id
|
||||||
|
|
||||||
async def async_update(self):
|
|
||||||
"""Update status of cover."""
|
|
||||||
await self._coordinator.async_request_refresh()
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def device_info(self):
|
def device_info(self):
|
||||||
"""Return the device_info of the device."""
|
"""Return the device_info of the device."""
|
||||||
|
@ -87,14 +89,3 @@ class MyQBinarySensorEntity(BinarySensorEntity):
|
||||||
device_info["model"] = model
|
device_info["model"] = model
|
||||||
|
|
||||||
return device_info
|
return device_info
|
||||||
|
|
||||||
@property
|
|
||||||
def should_poll(self):
|
|
||||||
"""Return False, updates are controlled via coordinator."""
|
|
||||||
return False
|
|
||||||
|
|
||||||
async def async_added_to_hass(self):
|
|
||||||
"""Subscribe to updates."""
|
|
||||||
self.async_on_remove(
|
|
||||||
self._coordinator.async_add_listener(self.async_write_ha_state)
|
|
||||||
)
|
|
||||||
|
|
|
@ -32,6 +32,7 @@ from homeassistant.const import (
|
||||||
from homeassistant.core import callback
|
from homeassistant.core import callback
|
||||||
from homeassistant.helpers import config_validation as cv
|
from homeassistant.helpers import config_validation as cv
|
||||||
from homeassistant.helpers.event import async_call_later
|
from homeassistant.helpers.event import async_call_later
|
||||||
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
|
|
||||||
from .const import (
|
from .const import (
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
|
@ -82,12 +83,12 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class MyQDevice(CoverEntity):
|
class MyQDevice(CoordinatorEntity, CoverEntity):
|
||||||
"""Representation of a MyQ cover."""
|
"""Representation of a MyQ cover."""
|
||||||
|
|
||||||
def __init__(self, coordinator, device):
|
def __init__(self, coordinator, device):
|
||||||
"""Initialize with API object, device id."""
|
"""Initialize with API object, device id."""
|
||||||
self._coordinator = coordinator
|
super().__init__(coordinator)
|
||||||
self._device = device
|
self._device = device
|
||||||
self._last_action_timestamp = 0
|
self._last_action_timestamp = 0
|
||||||
self._scheduled_transition_update = None
|
self._scheduled_transition_update = None
|
||||||
|
@ -108,7 +109,7 @@ class MyQDevice(CoverEntity):
|
||||||
@property
|
@property
|
||||||
def available(self):
|
def available(self):
|
||||||
"""Return if the device is online."""
|
"""Return if the device is online."""
|
||||||
if not self._coordinator.last_update_success:
|
if not self.coordinator.last_update_success:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# Not all devices report online so assume True if its missing
|
# Not all devices report online so assume True if its missing
|
||||||
|
@ -173,11 +174,7 @@ class MyQDevice(CoverEntity):
|
||||||
async def _async_complete_schedule_update(self, _):
|
async def _async_complete_schedule_update(self, _):
|
||||||
"""Update status of the cover via coordinator."""
|
"""Update status of the cover via coordinator."""
|
||||||
self._scheduled_transition_update = None
|
self._scheduled_transition_update = None
|
||||||
await self._coordinator.async_request_refresh()
|
await self.coordinator.async_request_refresh()
|
||||||
|
|
||||||
async def async_update(self):
|
|
||||||
"""Update status of cover."""
|
|
||||||
await self._coordinator.async_request_refresh()
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def device_info(self):
|
def device_info(self):
|
||||||
|
@ -204,15 +201,10 @@ class MyQDevice(CoverEntity):
|
||||||
|
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
@property
|
|
||||||
def should_poll(self):
|
|
||||||
"""Return False, updates are controlled via coordinator."""
|
|
||||||
return False
|
|
||||||
|
|
||||||
async def async_added_to_hass(self):
|
async def async_added_to_hass(self):
|
||||||
"""Subscribe to updates."""
|
"""Subscribe to updates."""
|
||||||
self.async_on_remove(
|
self.async_on_remove(
|
||||||
self._coordinator.async_add_listener(self._async_consume_update)
|
self.coordinator.async_add_listener(self._async_consume_update)
|
||||||
)
|
)
|
||||||
|
|
||||||
async def async_will_remove_from_hass(self):
|
async def async_will_remove_from_hass(self):
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue