mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
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:
parent
e707b50658
commit
159f6750d7
@ -7,7 +7,10 @@ from ProgettiHWSW.input import Input
|
|||||||
import async_timeout
|
import async_timeout
|
||||||
|
|
||||||
from homeassistant.components.binary_sensor import BinarySensorEntity
|
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 . import setup_input
|
||||||
from .const import DEFAULT_POLLING_INTERVAL_SEC, DOMAIN
|
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)
|
async_add_entities(binary_sensors)
|
||||||
|
|
||||||
|
|
||||||
class ProgettihwswBinarySensor(BinarySensorEntity):
|
class ProgettihwswBinarySensor(CoordinatorEntity, BinarySensorEntity):
|
||||||
"""Represent a binary sensor."""
|
"""Represent a binary sensor."""
|
||||||
|
|
||||||
def __init__(self, hass, coordinator, config_entry, name, sensor: Input):
|
def __init__(self, hass, coordinator, config_entry, name, sensor: Input):
|
||||||
"""Set initializing values."""
|
"""Set initializing values."""
|
||||||
|
super().__init__(coordinator)
|
||||||
self._name = name
|
self._name = name
|
||||||
self._sensor = sensor
|
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
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
@ -78,18 +75,4 @@ class ProgettihwswBinarySensor(BinarySensorEntity):
|
|||||||
@property
|
@property
|
||||||
def is_on(self):
|
def is_on(self):
|
||||||
"""Get sensor state."""
|
"""Get sensor state."""
|
||||||
return self._coordinator.data[self._sensor.id]
|
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()
|
|
||||||
|
@ -7,7 +7,10 @@ from ProgettiHWSW.relay import Relay
|
|||||||
import async_timeout
|
import async_timeout
|
||||||
|
|
||||||
from homeassistant.components.switch import SwitchEntity
|
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 . import setup_switch
|
||||||
from .const import DEFAULT_POLLING_INTERVAL_SEC, DOMAIN
|
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)
|
async_add_entities(switches)
|
||||||
|
|
||||||
|
|
||||||
class ProgettihwswSwitch(SwitchEntity):
|
class ProgettihwswSwitch(CoordinatorEntity, SwitchEntity):
|
||||||
"""Represent a switch entity."""
|
"""Represent a switch entity."""
|
||||||
|
|
||||||
def __init__(self, hass, coordinator, config_entry, name, switch: Relay):
|
def __init__(self, hass, coordinator, config_entry, name, switch: Relay):
|
||||||
"""Initialize the values."""
|
"""Initialize the values."""
|
||||||
|
super().__init__(coordinator)
|
||||||
self._switch = switch
|
self._switch = switch
|
||||||
self._name = name
|
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):
|
async def async_turn_on(self, **kwargs):
|
||||||
"""Turn the switch on."""
|
"""Turn the switch on."""
|
||||||
await self._switch.control(True)
|
await self._switch.control(True)
|
||||||
await self._coordinator.async_request_refresh()
|
await self.coordinator.async_request_refresh()
|
||||||
|
|
||||||
async def async_turn_off(self, **kwargs):
|
async def async_turn_off(self, **kwargs):
|
||||||
"""Turn the switch off."""
|
"""Turn the switch off."""
|
||||||
await self._switch.control(False)
|
await self._switch.control(False)
|
||||||
await self._coordinator.async_request_refresh()
|
await self.coordinator.async_request_refresh()
|
||||||
|
|
||||||
async def async_toggle(self, **kwargs):
|
async def async_toggle(self, **kwargs):
|
||||||
"""Toggle the state of switch."""
|
"""Toggle the state of switch."""
|
||||||
await self._switch.toggle()
|
await self._switch.toggle()
|
||||||
await self._coordinator.async_request_refresh()
|
await self.coordinator.async_request_refresh()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
@ -92,18 +89,4 @@ class ProgettihwswSwitch(SwitchEntity):
|
|||||||
@property
|
@property
|
||||||
def is_on(self):
|
def is_on(self):
|
||||||
"""Get switch state."""
|
"""Get switch state."""
|
||||||
return self._coordinator.data[self._switch.id]
|
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()
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user