Update myq to use CoordinatorEntity (#39393)

This commit is contained in:
J. Nick Koston 2020-08-30 07:40:00 -05:00 committed by GitHub
parent a4f475245c
commit 4e876cb473
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 32 deletions

View file

@ -14,6 +14,7 @@ from homeassistant.components.binary_sensor import (
DEVICE_CLASS_CONNECTIVITY,
BinarySensorEntity,
)
from homeassistant.helpers.update_coordinator import CoordinatorEntity
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)
class MyQBinarySensorEntity(BinarySensorEntity):
class MyQBinarySensorEntity(CoordinatorEntity, BinarySensorEntity):
"""Representation of a MyQ gateway."""
def __init__(self, coordinator, device):
"""Initialize with API object, device id."""
self._coordinator = coordinator
super().__init__(coordinator)
self._device = device
@property
@ -56,7 +57,7 @@ class MyQBinarySensorEntity(BinarySensorEntity):
@property
def is_on(self):
"""Return if the device is online."""
if not self._coordinator.last_update_success:
if not self.coordinator.last_update_success:
return False
# Not all devices report online so assume True if its missing
@ -64,15 +65,16 @@ class MyQBinarySensorEntity(BinarySensorEntity):
MYQ_DEVICE_STATE_ONLINE, True
)
@property
def available(self) -> bool:
"""Entity is always available."""
return True
@property
def unique_id(self):
"""Return a unique, Home Assistant friendly identifier for this entity."""
return self._device.device_id
async def async_update(self):
"""Update status of cover."""
await self._coordinator.async_request_refresh()
@property
def device_info(self):
"""Return the device_info of the device."""
@ -87,14 +89,3 @@ class MyQBinarySensorEntity(BinarySensorEntity):
device_info["model"] = model
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)
)

View file

@ -32,6 +32,7 @@ from homeassistant.const import (
from homeassistant.core import callback
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.event import async_call_later
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import (
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."""
def __init__(self, coordinator, device):
"""Initialize with API object, device id."""
self._coordinator = coordinator
super().__init__(coordinator)
self._device = device
self._last_action_timestamp = 0
self._scheduled_transition_update = None
@ -108,7 +109,7 @@ class MyQDevice(CoverEntity):
@property
def available(self):
"""Return if the device is online."""
if not self._coordinator.last_update_success:
if not self.coordinator.last_update_success:
return False
# 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, _):
"""Update status of the cover via coordinator."""
self._scheduled_transition_update = None
await self._coordinator.async_request_refresh()
async def async_update(self):
"""Update status of cover."""
await self._coordinator.async_request_refresh()
await self.coordinator.async_request_refresh()
@property
def device_info(self):
@ -204,15 +201,10 @@ class MyQDevice(CoverEntity):
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):
"""Subscribe to updates."""
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):