Move fetching of sw_version for Twinkly (#100286)

pull/100952/head
Olen 2023-09-26 20:04:17 +02:00 committed by GitHub
parent c7e4604cfd
commit e921e4a662
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 26 additions and 16 deletions

View File

@ -6,12 +6,12 @@ from aiohttp import ClientError
from ttls.client import Twinkly from ttls.client import Twinkly
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform from homeassistant.const import ATTR_SW_VERSION, Platform
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.aiohttp_client import async_get_clientsession
from .const import CONF_HOST, DATA_CLIENT, DATA_DEVICE_INFO, DOMAIN from .const import ATTR_VERSION, CONF_HOST, DATA_CLIENT, DATA_DEVICE_INFO, DOMAIN
PLATFORMS = [Platform.LIGHT] PLATFORMS = [Platform.LIGHT]
@ -30,12 +30,15 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
try: try:
device_info = await client.get_details() device_info = await client.get_details()
software_version = await client.get_firmware_version()
except (asyncio.TimeoutError, ClientError) as exception: except (asyncio.TimeoutError, ClientError) as exception:
raise ConfigEntryNotReady from exception raise ConfigEntryNotReady from exception
hass.data[DOMAIN][entry.entry_id][DATA_CLIENT] = client hass.data[DOMAIN][entry.entry_id] = {
hass.data[DOMAIN][entry.entry_id][DATA_DEVICE_INFO] = device_info DATA_CLIENT: client,
DATA_DEVICE_INFO: device_info,
ATTR_SW_VERSION: software_version.get(ATTR_VERSION),
}
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
return True return True

View File

@ -6,7 +6,7 @@ from typing import Any
from homeassistant.components.diagnostics import async_redact_data from homeassistant.components.diagnostics import async_redact_data
from homeassistant.components.light import DOMAIN as LIGHT_DOMAIN from homeassistant.components.light import DOMAIN as LIGHT_DOMAIN
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_HOST, CONF_IP_ADDRESS, CONF_MAC from homeassistant.const import ATTR_SW_VERSION, CONF_HOST, CONF_IP_ADDRESS, CONF_MAC
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er from homeassistant.helpers import entity_registry as er
@ -34,6 +34,7 @@ async def async_get_config_entry_diagnostics(
{ {
"entry": entry.as_dict(), "entry": entry.as_dict(),
"device_info": hass.data[DOMAIN][entry.entry_id][DATA_DEVICE_INFO], "device_info": hass.data[DOMAIN][entry.entry_id][DATA_DEVICE_INFO],
ATTR_SW_VERSION: hass.data[DOMAIN][entry.entry_id][ATTR_SW_VERSION],
"attributes": attributes, "attributes": attributes,
}, },
TO_REDACT, TO_REDACT,

View File

@ -19,13 +19,13 @@ from homeassistant.components.light import (
LightEntityFeature, LightEntityFeature,
) )
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_MODEL from homeassistant.const import ATTR_SW_VERSION, CONF_MODEL
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.device_registry import DeviceInfo from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import ( from .const import (
ATTR_VERSION,
CONF_HOST, CONF_HOST,
CONF_ID, CONF_ID,
CONF_NAME, CONF_NAME,
@ -52,8 +52,9 @@ async def async_setup_entry(
client = hass.data[DOMAIN][config_entry.entry_id][DATA_CLIENT] client = hass.data[DOMAIN][config_entry.entry_id][DATA_CLIENT]
device_info = hass.data[DOMAIN][config_entry.entry_id][DATA_DEVICE_INFO] device_info = hass.data[DOMAIN][config_entry.entry_id][DATA_DEVICE_INFO]
software_version = hass.data[DOMAIN][config_entry.entry_id][ATTR_SW_VERSION]
entity = TwinklyLight(config_entry, client, device_info) entity = TwinklyLight(config_entry, client, device_info, software_version)
async_add_entities([entity], update_before_add=True) async_add_entities([entity], update_before_add=True)
@ -68,6 +69,7 @@ class TwinklyLight(LightEntity):
conf: ConfigEntry, conf: ConfigEntry,
client: Twinkly, client: Twinkly,
device_info, device_info,
software_version: str | None = None,
) -> None: ) -> None:
"""Initialize a TwinklyLight entity.""" """Initialize a TwinklyLight entity."""
self._attr_unique_id: str = conf.data[CONF_ID] self._attr_unique_id: str = conf.data[CONF_ID]
@ -98,7 +100,7 @@ class TwinklyLight(LightEntity):
self._attr_available = False self._attr_available = False
self._current_movie: dict[Any, Any] = {} self._current_movie: dict[Any, Any] = {}
self._movies: list[Any] = [] self._movies: list[Any] = []
self._software_version = "" self._software_version = software_version
# We guess that most devices are "new" and support effects # We guess that most devices are "new" and support effects
self._attr_supported_features = LightEntityFeature.EFFECT self._attr_supported_features = LightEntityFeature.EFFECT
@ -135,16 +137,21 @@ class TwinklyLight(LightEntity):
async def async_added_to_hass(self) -> None: async def async_added_to_hass(self) -> None:
"""Device is added to hass.""" """Device is added to hass."""
software_version = await self._client.get_firmware_version() if self._software_version:
if ATTR_VERSION in software_version:
self._software_version = software_version[ATTR_VERSION]
if AwesomeVersion(self._software_version) < AwesomeVersion( if AwesomeVersion(self._software_version) < AwesomeVersion(
MIN_EFFECT_VERSION MIN_EFFECT_VERSION
): ):
self._attr_supported_features = ( self._attr_supported_features = (
self.supported_features & ~LightEntityFeature.EFFECT self.supported_features & ~LightEntityFeature.EFFECT
) )
device_registry = dr.async_get(self.hass)
device_entry = device_registry.async_get_device(
{(DOMAIN, self._attr_unique_id)}, set()
)
if device_entry:
device_registry.async_update_device(
device_entry.id, sw_version=self._software_version
)
async def async_turn_on(self, **kwargs: Any) -> None: async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn device on.""" """Turn device on."""

View File

@ -33,7 +33,6 @@ class ClientMock:
"uuid": self.id, "uuid": self.id,
"device_name": TEST_NAME, "device_name": TEST_NAME,
"product_code": TEST_MODEL, "product_code": TEST_MODEL,
"sw_version": self.version,
} }
@property @property

View File

@ -16,7 +16,6 @@
'device_info': dict({ 'device_info': dict({
'device_name': 'twinkly_test_device_name', 'device_name': 'twinkly_test_device_name',
'product_code': 'twinkly_test_device_model', 'product_code': 'twinkly_test_device_model',
'sw_version': '2.8.10',
'uuid': '4c8fccf5-e08a-4173-92d5-49bf479252a2', 'uuid': '4c8fccf5-e08a-4173-92d5-49bf479252a2',
}), }),
'entry': dict({ 'entry': dict({
@ -39,5 +38,6 @@
'unique_id': '4c8fccf5-e08a-4173-92d5-49bf479252a2', 'unique_id': '4c8fccf5-e08a-4173-92d5-49bf479252a2',
'version': 1, 'version': 1,
}), }),
'sw_version': '2.8.10',
}) })
# --- # ---