Remove unnecessary awaits in RainMachine (#32884)

* Remove unnecessary awaits in RainMachine

* Cleanup
This commit is contained in:
Aaron Bach 2020-03-17 05:00:54 -06:00 committed by GitHub
parent abd1909e2b
commit 0c49c8578b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 17 deletions

View file

@ -455,9 +455,16 @@ class RainMachineEntity(Entity):
@callback
def _update_state(self):
"""Update the state."""
self.async_schedule_update_ha_state(True)
self.update_from_latest_data()
self.async_write_ha_state()
async def async_will_remove_from_hass(self):
"""Disconnect dispatcher listener when removed."""
for handler in self._dispatcher_handlers:
handler()
self._dispatcher_handlers = []
@callback
def update_from_latest_data(self):
"""Update the entity."""
raise NotImplementedError

View file

@ -2,6 +2,7 @@
import logging
from homeassistant.components.binary_sensor import BinarySensorDevice
from homeassistant.core import callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from . import RainMachineEntity
@ -129,9 +130,15 @@ class RainMachineBinarySensor(RainMachineEntity, BinarySensorDevice):
async_dispatcher_connect(self.hass, SENSOR_UPDATE_TOPIC, self._update_state)
)
await self.rainmachine.async_register_sensor_api_interest(self._api_category)
await self.async_update()
self.update_from_latest_data()
async def async_update(self):
async def async_will_remove_from_hass(self):
"""Disconnect dispatcher listeners and deregister API interest."""
super().async_will_remove_from_hass()
self.rainmachine.async_deregister_sensor_api_interest(self._api_category)
@callback
def update_from_latest_data(self):
"""Update the state."""
if self._sensor_type == TYPE_FLOW_SENSOR:
self._state = self.rainmachine.data[DATA_PROVISION_SETTINGS]["system"].get(
@ -157,8 +164,3 @@ class RainMachineBinarySensor(RainMachineEntity, BinarySensorDevice):
self._state = self.rainmachine.data[DATA_RESTRICTIONS_CURRENT]["rainSensor"]
elif self._sensor_type == TYPE_WEEKDAY:
self._state = self.rainmachine.data[DATA_RESTRICTIONS_CURRENT]["weekDay"]
async def async_will_remove_from_hass(self):
"""Disconnect dispatcher listeners and deregister API interest."""
super().async_will_remove_from_hass()
self.rainmachine.async_deregister_sensor_api_interest(self._api_category)

View file

@ -1,6 +1,7 @@
"""This platform provides support for sensor data from RainMachine."""
import logging
from homeassistant.core import callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from . import RainMachineEntity
@ -146,9 +147,15 @@ class RainMachineSensor(RainMachineEntity):
async_dispatcher_connect(self.hass, SENSOR_UPDATE_TOPIC, self._update_state)
)
await self.rainmachine.async_register_sensor_api_interest(self._api_category)
await self.async_update()
self.update_from_latest_data()
async def async_update(self):
async def async_will_remove_from_hass(self):
"""Disconnect dispatcher listeners and deregister API interest."""
super().async_will_remove_from_hass()
self.rainmachine.async_deregister_sensor_api_interest(self._api_category)
@callback
def update_from_latest_data(self):
"""Update the sensor's state."""
if self._sensor_type == TYPE_FLOW_SENSOR_CLICK_M3:
self._state = self.rainmachine.data[DATA_PROVISION_SETTINGS]["system"].get(
@ -178,8 +185,3 @@ class RainMachineSensor(RainMachineEntity):
self._state = self.rainmachine.data[DATA_RESTRICTIONS_UNIVERSAL][
"freezeProtectTemp"
]
async def async_will_remove_from_hass(self):
"""Disconnect dispatcher listeners and deregister API interest."""
super().async_will_remove_from_hass()
self.rainmachine.async_deregister_sensor_api_interest(self._api_category)

View file

@ -6,6 +6,7 @@ from regenmaschine.errors import RequestError
from homeassistant.components.switch import SwitchDevice
from homeassistant.const import ATTR_ID
from homeassistant.core import callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from . import RainMachineEntity
@ -205,7 +206,8 @@ class RainMachineProgram(RainMachineSwitch):
self.rainmachine.controller.programs.start(self._rainmachine_entity_id)
)
async def async_update(self) -> None:
@callback
def update_from_latest_data(self) -> None:
"""Update info for the program."""
[self._switch_data] = [
p
@ -269,7 +271,8 @@ class RainMachineZone(RainMachineSwitch):
)
)
async def async_update(self) -> None:
@callback
def update_from_latest_data(self) -> None:
"""Update info for the zone."""
[self._switch_data] = [
z