Move fetching of sw_version for Twinkly (#100286)
parent
c7e4604cfd
commit
e921e4a662
|
@ -6,12 +6,12 @@ from aiohttp import ClientError
|
|||
from ttls.client import Twinkly
|
||||
|
||||
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.exceptions import ConfigEntryNotReady
|
||||
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]
|
||||
|
||||
|
@ -30,12 +30,15 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||
|
||||
try:
|
||||
device_info = await client.get_details()
|
||||
software_version = await client.get_firmware_version()
|
||||
except (asyncio.TimeoutError, ClientError) as exception:
|
||||
raise ConfigEntryNotReady from exception
|
||||
|
||||
hass.data[DOMAIN][entry.entry_id][DATA_CLIENT] = client
|
||||
hass.data[DOMAIN][entry.entry_id][DATA_DEVICE_INFO] = device_info
|
||||
|
||||
hass.data[DOMAIN][entry.entry_id] = {
|
||||
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)
|
||||
|
||||
return True
|
||||
|
|
|
@ -6,7 +6,7 @@ from typing import Any
|
|||
from homeassistant.components.diagnostics import async_redact_data
|
||||
from homeassistant.components.light import DOMAIN as LIGHT_DOMAIN
|
||||
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.helpers import entity_registry as er
|
||||
|
||||
|
@ -34,6 +34,7 @@ async def async_get_config_entry_diagnostics(
|
|||
{
|
||||
"entry": entry.as_dict(),
|
||||
"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,
|
||||
},
|
||||
TO_REDACT,
|
||||
|
|
|
@ -19,13 +19,13 @@ from homeassistant.components.light import (
|
|||
LightEntityFeature,
|
||||
)
|
||||
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.helpers import device_registry as dr
|
||||
from homeassistant.helpers.device_registry import DeviceInfo
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from .const import (
|
||||
ATTR_VERSION,
|
||||
CONF_HOST,
|
||||
CONF_ID,
|
||||
CONF_NAME,
|
||||
|
@ -52,8 +52,9 @@ async def async_setup_entry(
|
|||
|
||||
client = hass.data[DOMAIN][config_entry.entry_id][DATA_CLIENT]
|
||||
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)
|
||||
|
||||
|
@ -68,6 +69,7 @@ class TwinklyLight(LightEntity):
|
|||
conf: ConfigEntry,
|
||||
client: Twinkly,
|
||||
device_info,
|
||||
software_version: str | None = None,
|
||||
) -> None:
|
||||
"""Initialize a TwinklyLight entity."""
|
||||
self._attr_unique_id: str = conf.data[CONF_ID]
|
||||
|
@ -98,7 +100,7 @@ class TwinklyLight(LightEntity):
|
|||
self._attr_available = False
|
||||
self._current_movie: dict[Any, Any] = {}
|
||||
self._movies: list[Any] = []
|
||||
self._software_version = ""
|
||||
self._software_version = software_version
|
||||
# We guess that most devices are "new" and support effects
|
||||
self._attr_supported_features = LightEntityFeature.EFFECT
|
||||
|
||||
|
@ -135,16 +137,21 @@ class TwinklyLight(LightEntity):
|
|||
|
||||
async def async_added_to_hass(self) -> None:
|
||||
"""Device is added to hass."""
|
||||
software_version = await self._client.get_firmware_version()
|
||||
if ATTR_VERSION in software_version:
|
||||
self._software_version = software_version[ATTR_VERSION]
|
||||
|
||||
if self._software_version:
|
||||
if AwesomeVersion(self._software_version) < AwesomeVersion(
|
||||
MIN_EFFECT_VERSION
|
||||
):
|
||||
self._attr_supported_features = (
|
||||
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:
|
||||
"""Turn device on."""
|
||||
|
|
|
@ -33,7 +33,6 @@ class ClientMock:
|
|||
"uuid": self.id,
|
||||
"device_name": TEST_NAME,
|
||||
"product_code": TEST_MODEL,
|
||||
"sw_version": self.version,
|
||||
}
|
||||
|
||||
@property
|
||||
|
|
|
@ -16,7 +16,6 @@
|
|||
'device_info': dict({
|
||||
'device_name': 'twinkly_test_device_name',
|
||||
'product_code': 'twinkly_test_device_model',
|
||||
'sw_version': '2.8.10',
|
||||
'uuid': '4c8fccf5-e08a-4173-92d5-49bf479252a2',
|
||||
}),
|
||||
'entry': dict({
|
||||
|
@ -39,5 +38,6 @@
|
|||
'unique_id': '4c8fccf5-e08a-4173-92d5-49bf479252a2',
|
||||
'version': 1,
|
||||
}),
|
||||
'sw_version': '2.8.10',
|
||||
})
|
||||
# ---
|
||||
|
|
Loading…
Reference in New Issue