Add cleanup to OpenUV (#32886)

* Add cleanup to OpenUV

* Linting
This commit is contained in:
Aaron Bach 2020-03-17 21:03:16 -06:00 committed by GitHub
parent f02c5f66d6
commit 609263e1bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 42 deletions

View File

@ -16,9 +16,13 @@ from homeassistant.const import (
CONF_LONGITUDE,
CONF_SENSORS,
)
from homeassistant.core import callback
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers import aiohttp_client, config_validation as cv
from homeassistant.helpers.dispatcher import async_dispatcher_send
from homeassistant.helpers.dispatcher import (
async_dispatcher_connect,
async_dispatcher_send,
)
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.service import verify_domain_control
@ -104,7 +108,6 @@ async def async_setup(hass, config):
async def async_setup_entry(hass, config_entry):
"""Set up OpenUV as config entry."""
_verify_domain_control = verify_domain_control(hass, DOMAIN)
try:
@ -230,6 +233,7 @@ class OpenUvEntity(Entity):
def __init__(self, openuv):
"""Initialize."""
self._async_unsub_dispatcher_connect = None
self._attrs = {ATTR_ATTRIBUTION: DEFAULT_ATTRIBUTION}
self._available = True
self._name = None
@ -249,3 +253,28 @@ class OpenUvEntity(Entity):
def name(self):
"""Return the name of the entity."""
return self._name
async def async_added_to_hass(self):
"""Register callbacks."""
@callback
def update():
"""Update the state."""
self.update_from_latest_data()
self.async_write_ha_state()
self._async_unsub_dispatcher_connect = async_dispatcher_connect(
self.hass, TOPIC_UPDATE, update
)
self.update_from_latest_data()
async def async_will_remove_from_hass(self):
"""Disconnect dispatcher listener when removed."""
if self._async_unsub_dispatcher_connect:
self._async_unsub_dispatcher_connect()
self._async_unsub_dispatcher_connect = None
def update_from_latest_data(self):
"""Update the sensor using the latest data."""
raise NotImplementedError

View File

@ -3,14 +3,12 @@ import logging
from homeassistant.components.binary_sensor import BinarySensorDevice
from homeassistant.core import callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.util.dt import as_local, parse_datetime, utcnow
from . import (
DATA_OPENUV_CLIENT,
DATA_PROTECTION_WINDOW,
DOMAIN,
TOPIC_UPDATE,
TYPE_PROTECTION_WINDOW,
OpenUvEntity,
)
@ -75,24 +73,8 @@ class OpenUvBinarySensor(OpenUvEntity, BinarySensorDevice):
"""Return a unique, Home Assistant friendly identifier for this entity."""
return f"{self._latitude}_{self._longitude}_{self._sensor_type}"
async def async_added_to_hass(self):
"""Register callbacks."""
@callback
def update():
"""Update the state."""
self.async_schedule_update_ha_state(True)
self._async_unsub_dispatcher_connect = async_dispatcher_connect(
self.hass, TOPIC_UPDATE, update
)
async def async_will_remove_from_hass(self):
"""Disconnect dispatcher listener when removed."""
if self._async_unsub_dispatcher_connect:
self._async_unsub_dispatcher_connect()
async def async_update(self):
@callback
def update_from_latest_data(self):
"""Update the state."""
data = self.openuv.data[DATA_PROTECTION_WINDOW]

View File

@ -3,14 +3,12 @@ import logging
from homeassistant.const import TIME_MINUTES
from homeassistant.core import callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.util.dt import as_local, parse_datetime
from . import (
DATA_OPENUV_CLIENT,
DATA_UV,
DOMAIN,
TOPIC_UPDATE,
TYPE_CURRENT_OZONE_LEVEL,
TYPE_CURRENT_UV_INDEX,
TYPE_CURRENT_UV_LEVEL,
@ -135,24 +133,8 @@ class OpenUvSensor(OpenUvEntity):
"""Return the unit the value is expressed in."""
return self._unit
async def async_added_to_hass(self):
"""Register callbacks."""
@callback
def update():
"""Update the state."""
self.async_schedule_update_ha_state(True)
self._async_unsub_dispatcher_connect = async_dispatcher_connect(
self.hass, TOPIC_UPDATE, update
)
async def async_will_remove_from_hass(self):
"""Disconnect dispatcher listener when removed."""
if self._async_unsub_dispatcher_connect:
self._async_unsub_dispatcher_connect()
async def async_update(self):
@callback
def update_from_latest_data(self):
"""Update the state."""
data = self.openuv.data[DATA_UV].get("result")