mirror of
https://github.com/home-assistant/core.git
synced 2025-07-26 22:57:17 +00:00
Opengarage dataupdater (#56931)
This commit is contained in:
parent
8f6ed2d27e
commit
e9d601a688
@ -1,15 +1,21 @@
|
|||||||
"""The OpenGarage integration."""
|
"""The OpenGarage integration."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from datetime import timedelta
|
||||||
|
import logging
|
||||||
|
|
||||||
import opengarage
|
import opengarage
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import CONF_HOST, CONF_PORT, CONF_VERIFY_SSL
|
from homeassistant.const import CONF_HOST, CONF_PORT, CONF_VERIFY_SSL
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.helpers import update_coordinator
|
||||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||||
|
|
||||||
from .const import CONF_DEVICE_KEY, DOMAIN
|
from .const import CONF_DEVICE_KEY, DOMAIN
|
||||||
|
|
||||||
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
PLATFORMS = ["cover"]
|
PLATFORMS = ["cover"]
|
||||||
|
|
||||||
|
|
||||||
@ -17,12 +23,18 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
"""Set up OpenGarage from a config entry."""
|
"""Set up OpenGarage from a config entry."""
|
||||||
hass.data.setdefault(DOMAIN, {})
|
hass.data.setdefault(DOMAIN, {})
|
||||||
|
|
||||||
hass.data[DOMAIN][entry.entry_id] = opengarage.OpenGarage(
|
open_garage_connection = opengarage.OpenGarage(
|
||||||
f"{entry.data[CONF_HOST]}:{entry.data[CONF_PORT]}",
|
f"{entry.data[CONF_HOST]}:{entry.data[CONF_PORT]}",
|
||||||
entry.data[CONF_DEVICE_KEY],
|
entry.data[CONF_DEVICE_KEY],
|
||||||
entry.data[CONF_VERIFY_SSL],
|
entry.data[CONF_VERIFY_SSL],
|
||||||
async_get_clientsession(hass),
|
async_get_clientsession(hass),
|
||||||
)
|
)
|
||||||
|
open_garage_data_coordinator = OpenGarageDataUpdateCoordinator(
|
||||||
|
hass,
|
||||||
|
open_garage_connection=open_garage_connection,
|
||||||
|
)
|
||||||
|
await open_garage_data_coordinator.async_config_entry_first_refresh()
|
||||||
|
hass.data[DOMAIN][entry.entry_id] = open_garage_data_coordinator
|
||||||
|
|
||||||
hass.config_entries.async_setup_platforms(entry, PLATFORMS)
|
hass.config_entries.async_setup_platforms(entry, PLATFORMS)
|
||||||
|
|
||||||
@ -36,3 +48,32 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
hass.data[DOMAIN].pop(entry.entry_id)
|
hass.data[DOMAIN].pop(entry.entry_id)
|
||||||
|
|
||||||
return unload_ok
|
return unload_ok
|
||||||
|
|
||||||
|
|
||||||
|
class OpenGarageDataUpdateCoordinator(update_coordinator.DataUpdateCoordinator):
|
||||||
|
"""Class to manage fetching Opengarage data."""
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
hass: HomeAssistant,
|
||||||
|
*,
|
||||||
|
open_garage_connection: opengarage.OpenGarage,
|
||||||
|
) -> None:
|
||||||
|
"""Initialize global Opengarage data updater."""
|
||||||
|
self.open_garage_connection = open_garage_connection
|
||||||
|
|
||||||
|
super().__init__(
|
||||||
|
hass,
|
||||||
|
_LOGGER,
|
||||||
|
name=DOMAIN,
|
||||||
|
update_interval=timedelta(seconds=30),
|
||||||
|
)
|
||||||
|
|
||||||
|
async def _async_update_data(self) -> None:
|
||||||
|
"""Fetch data."""
|
||||||
|
data = await self.open_garage_connection.update_state()
|
||||||
|
if data is None:
|
||||||
|
raise update_coordinator.UpdateFailed(
|
||||||
|
"Unable to connect to OpenGarage device"
|
||||||
|
)
|
||||||
|
return data
|
||||||
|
@ -23,8 +23,10 @@ from homeassistant.const import (
|
|||||||
STATE_OPEN,
|
STATE_OPEN,
|
||||||
STATE_OPENING,
|
STATE_OPENING,
|
||||||
)
|
)
|
||||||
|
from homeassistant.core import callback
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.helpers.entity import DeviceInfo
|
from homeassistant.helpers.entity import DeviceInfo
|
||||||
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
|
|
||||||
from .const import (
|
from .const import (
|
||||||
ATTR_DISTANCE_SENSOR,
|
ATTR_DISTANCE_SENSOR,
|
||||||
@ -75,28 +77,26 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
|
|||||||
async def async_setup_entry(hass, entry, async_add_entities):
|
async def async_setup_entry(hass, entry, async_add_entities):
|
||||||
"""Set up the OpenGarage covers."""
|
"""Set up the OpenGarage covers."""
|
||||||
async_add_entities(
|
async_add_entities(
|
||||||
[OpenGarageCover(hass.data[DOMAIN][entry.entry_id], entry.unique_id)], True
|
[OpenGarageCover(hass.data[DOMAIN][entry.entry_id], entry.unique_id)]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class OpenGarageCover(CoverEntity):
|
class OpenGarageCover(CoordinatorEntity, CoverEntity):
|
||||||
"""Representation of a OpenGarage cover."""
|
"""Representation of a OpenGarage cover."""
|
||||||
|
|
||||||
_attr_device_class = DEVICE_CLASS_GARAGE
|
_attr_device_class = DEVICE_CLASS_GARAGE
|
||||||
_attr_supported_features = SUPPORT_OPEN | SUPPORT_CLOSE
|
_attr_supported_features = SUPPORT_OPEN | SUPPORT_CLOSE
|
||||||
|
|
||||||
def __init__(self, open_garage, device_id):
|
def __init__(self, open_garage_data_coordinator, device_id):
|
||||||
"""Initialize the cover."""
|
"""Initialize the cover."""
|
||||||
self._open_garage = open_garage
|
super().__init__(open_garage_data_coordinator)
|
||||||
|
|
||||||
self._state = None
|
self._state = None
|
||||||
self._state_before_move = None
|
self._state_before_move = None
|
||||||
self._extra_state_attributes = {}
|
self._attr_extra_state_attributes = {}
|
||||||
self._attr_unique_id = self._device_id = device_id
|
self._attr_unique_id = self._device_id = device_id
|
||||||
|
self._device_name = None
|
||||||
@property
|
self._update_attr()
|
||||||
def extra_state_attributes(self):
|
|
||||||
"""Return the device state attributes."""
|
|
||||||
return self._extra_state_attributes
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_closed(self):
|
def is_closed(self):
|
||||||
@ -135,16 +135,16 @@ class OpenGarageCover(CoverEntity):
|
|||||||
self._state = STATE_OPENING
|
self._state = STATE_OPENING
|
||||||
await self._push_button()
|
await self._push_button()
|
||||||
|
|
||||||
async def async_update(self):
|
@callback
|
||||||
"""Get updated status from API."""
|
def _update_attr(self) -> None:
|
||||||
status = await self._open_garage.update_state()
|
"""Update the state and attributes."""
|
||||||
|
status = self.coordinator.data
|
||||||
if status is None:
|
if status is None:
|
||||||
_LOGGER.error("Unable to connect to OpenGarage device")
|
_LOGGER.error("Unable to connect to OpenGarage device")
|
||||||
self._attr_available = False
|
self._attr_available = False
|
||||||
return
|
return
|
||||||
|
|
||||||
if self.name is None and status["name"] is not None:
|
self._device_name = self._attr_name = status["name"]
|
||||||
self._attr_name = status["name"]
|
|
||||||
state = STATES_MAP.get(status.get("door"))
|
state = STATES_MAP.get(status.get("door"))
|
||||||
if self._state_before_move is not None:
|
if self._state_before_move is not None:
|
||||||
if self._state_before_move != state:
|
if self._state_before_move != state:
|
||||||
@ -155,17 +155,21 @@ class OpenGarageCover(CoverEntity):
|
|||||||
|
|
||||||
_LOGGER.debug("%s status: %s", self.name, self._state)
|
_LOGGER.debug("%s status: %s", self.name, self._state)
|
||||||
if status.get("rssi") is not None:
|
if status.get("rssi") is not None:
|
||||||
self._extra_state_attributes[ATTR_SIGNAL_STRENGTH] = status.get("rssi")
|
self._attr_extra_state_attributes[ATTR_SIGNAL_STRENGTH] = status.get("rssi")
|
||||||
if status.get("dist") is not None:
|
if status.get("dist") is not None:
|
||||||
self._extra_state_attributes[ATTR_DISTANCE_SENSOR] = status.get("dist")
|
self._attr_extra_state_attributes[ATTR_DISTANCE_SENSOR] = status.get("dist")
|
||||||
if self._state is not None:
|
if self._state is not None:
|
||||||
self._extra_state_attributes[ATTR_DOOR_STATE] = self._state
|
self._attr_extra_state_attributes[ATTR_DOOR_STATE] = self._state
|
||||||
|
|
||||||
self._attr_available = True
|
@callback
|
||||||
|
def _handle_coordinator_update(self) -> None:
|
||||||
|
"""Handle updated data from the coordinator."""
|
||||||
|
self._update_attr()
|
||||||
|
self.async_write_ha_state()
|
||||||
|
|
||||||
async def _push_button(self):
|
async def _push_button(self):
|
||||||
"""Send commands to API."""
|
"""Send commands to API."""
|
||||||
result = await self._open_garage.push_button()
|
result = await self.coordinator.open_garage_connection.push_button()
|
||||||
if result is None:
|
if result is None:
|
||||||
_LOGGER.error("Unable to connect to OpenGarage device")
|
_LOGGER.error("Unable to connect to OpenGarage device")
|
||||||
if result == 1:
|
if result == 1:
|
||||||
@ -184,7 +188,7 @@ class OpenGarageCover(CoverEntity):
|
|||||||
"""Return the device_info of the device."""
|
"""Return the device_info of the device."""
|
||||||
device_info = DeviceInfo(
|
device_info = DeviceInfo(
|
||||||
identifiers={(DOMAIN, self._device_id)},
|
identifiers={(DOMAIN, self._device_id)},
|
||||||
name=self.name,
|
name=self._device_name,
|
||||||
manufacturer="Open Garage",
|
manufacturer="Open Garage",
|
||||||
)
|
)
|
||||||
return device_info
|
return device_info
|
||||||
|
Loading…
x
Reference in New Issue
Block a user