Skip to content

Commit

Permalink
add firmware update entity
Browse files Browse the repository at this point in the history
  • Loading branch information
mampfes committed Sep 27, 2023
1 parent 6552c57 commit b9ac794
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 5 deletions.
17 changes: 14 additions & 3 deletions custom_components/bayernluefter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
"""

import logging
from datetime import timedelta
from datetime import timedelta, datetime
from typing import Any

from homeassistant.core import HomeAssistant
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_HOST, CONF_SCAN_INTERVAL
from homeassistant.const import CONF_HOST, CONF_SCAN_INTERVAL, Platform
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.device_registry import DeviceInfo, format_mac
from homeassistant.helpers.entity import Entity, EntityDescription
from homeassistant.helpers.event import async_track_time_interval
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
from homeassistant.helpers.update_coordinator import CoordinatorEntity

Expand All @@ -23,8 +24,9 @@


SCAN_INTERVAL = timedelta(seconds=20)
UPDATE_SCAN_INTERVAL = timedelta(days=1) # check once per day for firmware updates

PLATFORMS = ["sensor", "binary_sensor", "fan"]
PLATFORMS = [Platform.SENSOR, Platform.BINARY_SENSOR, Platform.FAN, Platform.UPDATE]


async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
Expand All @@ -33,6 +35,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
session = async_get_clientsession(hass)
device = Bayernluefter(entry.data[CONF_HOST], session)

await device.poll_latest_versions()

update_interval = timedelta(
seconds=entry.options.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL)
)
Expand All @@ -47,6 +51,13 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:

entry.async_on_unload(entry.add_update_listener(on_update_options_listener))

async def poll_latest_versions(now: datetime) -> None:
await device.poll_latest_versions()

entry.async_on_unload(
async_track_time_interval(hass, poll_latest_versions, UPDATE_SCAN_INTERVAL)
)

return True


Expand Down
4 changes: 2 additions & 2 deletions custom_components/bayernluefter/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
"integration_type": "device",
"iot_class": "local_polling",
"issue_tracker": "https://github.com/mampfes/ha_bayernluefter/issues",
"requirements": ["pyernluefter==0.1.1"],
"version": "1.1.0"
"requirements": ["pyernluefter==0.2.0"],
"version": "1.2.0"
}
73 changes: 73 additions & 0 deletions custom_components/bayernluefter/update.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
"""
Support for Bayernluefter update.
"""

import logging
from typing import Any

from homeassistant.components.update import (
UpdateEntity,
UpdateEntityDescription,
UpdateEntityFeature,
UpdateDeviceClass,
)

from pyernluefter import UpdateTarget

from . import (
BayernluefterEntity,
BayernluefterDataUpdateCoordinator as DataUpdateCoordinator,
)

from .const import DOMAIN

_LOGGER = logging.getLogger(__name__)


async def async_setup_entry(hass, config_entry, async_add_entities):
"""Set up update entries."""
coordinator: DataUpdateCoordinator = hass.data[DOMAIN][config_entry.entry_id]
entities = [BayernluefterUpdate(coordinator)]
async_add_entities(entities)


class BayernluefterUpdate(BayernluefterEntity, UpdateEntity):
"""An update implementation for Bayernluefter devices."""

entity_description = UpdateEntityDescription(
key="FW_WiFi", device_class=UpdateDeviceClass.FIRMWARE
)

target = UpdateTarget.WLAN32

# These update specific attributes are not (yet) part of UpdateEntityDescription
_attr_supported_features = UpdateEntityFeature.INSTALL

def __init__(
self,
coordinator: DataUpdateCoordinator,
) -> None:
"""Initialize an update entity for a Bayernluefter device."""
super().__init__(coordinator, self.entity_description)
self._attr_release_url = self._device.release_url(self.target)

@property
def available(self) -> bool:
return (
self._coordinator.last_update_success
and self._device.installed_version(self.target) is not None
)

@property
def latest_version(self) -> str:
return self._device.latest_version(self.target)

@property
def installed_version(self) -> str:
return self._device.installed_version(self.target)

async def async_install(
self, version: str | None, backup: bool, **kwargs: Any
) -> None:
"""Install the latest firmware version."""
await self._device.update_check(self.target)

0 comments on commit b9ac794

Please sign in to comment.