Update progettihwsw to use CoordinatorEntity (#39477)

* update progettihwsw to use CoordinatorEntity

* Update switch.py

* Update binary_sensor.py

* Update binary_sensor.py

* Update switch.py
This commit is contained in:
Chris Talkington 2020-08-30 15:53:32 -05:00 committed by GitHub
parent e707b50658
commit 159f6750d7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 51 deletions

View file

@ -7,7 +7,10 @@ from ProgettiHWSW.input import Input
import async_timeout
from homeassistant.components.binary_sensor import BinarySensorEntity
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
from homeassistant.helpers.update_coordinator import (
CoordinatorEntity,
DataUpdateCoordinator,
)
from . import setup_input
from .const import DEFAULT_POLLING_INTERVAL_SEC, DOMAIN
@ -55,20 +58,14 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
async_add_entities(binary_sensors)
class ProgettihwswBinarySensor(BinarySensorEntity):
class ProgettihwswBinarySensor(CoordinatorEntity, BinarySensorEntity):
"""Represent a binary sensor."""
def __init__(self, hass, coordinator, config_entry, name, sensor: Input):
"""Set initializing values."""
super().__init__(coordinator)
self._name = name
self._sensor = sensor
self._coordinator = coordinator
async def async_added_to_hass(self):
"""When entity is added to hass."""
self.async_on_remove(
self._coordinator.async_add_listener(self.async_write_ha_state)
)
@property
def name(self):
@ -78,18 +75,4 @@ class ProgettihwswBinarySensor(BinarySensorEntity):
@property
def is_on(self):
"""Get sensor state."""
return self._coordinator.data[self._sensor.id]
@property
def should_poll(self):
"""No need to poll. Coordinator notifies entity of updates."""
return False
@property
def available(self):
"""Return if entity is available."""
return self._coordinator.last_update_success
async def async_update(self):
"""Update the state of binary sensor."""
await self._coordinator.async_request_refresh()
return self.coordinator.data[self._sensor.id]

View file

@ -7,7 +7,10 @@ from ProgettiHWSW.relay import Relay
import async_timeout
from homeassistant.components.switch import SwitchEntity
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
from homeassistant.helpers.update_coordinator import (
CoordinatorEntity,
DataUpdateCoordinator,
)
from . import setup_switch
from .const import DEFAULT_POLLING_INTERVAL_SEC, DOMAIN
@ -54,35 +57,29 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
async_add_entities(switches)
class ProgettihwswSwitch(SwitchEntity):
class ProgettihwswSwitch(CoordinatorEntity, SwitchEntity):
"""Represent a switch entity."""
def __init__(self, hass, coordinator, config_entry, name, switch: Relay):
"""Initialize the values."""
super().__init__(coordinator)
self._switch = switch
self._name = name
self._coordinator = coordinator
async def async_added_to_hass(self):
"""When entity is added to hass."""
self.async_on_remove(
self._coordinator.async_add_listener(self.async_write_ha_state)
)
async def async_turn_on(self, **kwargs):
"""Turn the switch on."""
await self._switch.control(True)
await self._coordinator.async_request_refresh()
await self.coordinator.async_request_refresh()
async def async_turn_off(self, **kwargs):
"""Turn the switch off."""
await self._switch.control(False)
await self._coordinator.async_request_refresh()
await self.coordinator.async_request_refresh()
async def async_toggle(self, **kwargs):
"""Toggle the state of switch."""
await self._switch.toggle()
await self._coordinator.async_request_refresh()
await self.coordinator.async_request_refresh()
@property
def name(self):
@ -92,18 +89,4 @@ class ProgettihwswSwitch(SwitchEntity):
@property
def is_on(self):
"""Get switch state."""
return self._coordinator.data[self._switch.id]
@property
def should_poll(self):
"""No need to poll. Coordinator notifies entity of updates."""
return False
@property
def available(self):
"""Return if entity is available."""
return self._coordinator.last_update_success
async def async_update(self):
"""Update the state of switch."""
await self._coordinator.async_request_refresh()
return self.coordinator.data[self._switch.id]