From 4a0d86fff072b68df9cb95a375d89527d6fdde4f Mon Sep 17 00:00:00 2001 From: Nathan Spencer Date: Sat, 2 Dec 2023 17:37:11 +0000 Subject: [PATCH] Fix for car diffusers without fragrance name in data and allow deleting devices if no longer present --- custom_components/pura/__init__.py | 17 +++++++++++++++++ custom_components/pura/binary_sensor.py | 2 +- custom_components/pura/sensor.py | 4 ++-- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/custom_components/pura/__init__.py b/custom_components/pura/__init__.py index a8b902b..38fd573 100644 --- a/custom_components/pura/__init__.py +++ b/custom_components/pura/__init__.py @@ -7,9 +7,11 @@ from homeassistant.const import CONF_ACCESS_TOKEN, CONF_USERNAME, Platform from homeassistant.core import HomeAssistant from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady +from homeassistant.helpers.device_registry import DeviceEntry from .const import CONF_ID_TOKEN, CONF_REFRESH_TOKEN, DOMAIN from .coordinator import PuraDataUpdateCoordinator +from .helpers import get_device_id PLATFORMS = [ Platform.BINARY_SENSOR, @@ -63,3 +65,18 @@ async def async_remove_entry(hass: HomeAssistant, entry: ConfigEntry) -> None: async def update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None: """Handle options update.""" await hass.config_entries.async_reload(entry.entry_id) + + +async def async_remove_config_entry_device( + hass: HomeAssistant, config_entry: ConfigEntry, device_entry: DeviceEntry +) -> bool: + """Remove a config entry from a device.""" + coordinator: PuraDataUpdateCoordinator = hass.data[DOMAIN][config_entry.entry_id] + return not any( + identifier + for identifier in device_entry.identifiers + if identifier[0] == DOMAIN + for devices in coordinator.devices.values() + for device in devices + if identifier[1] == get_device_id(device) + ) diff --git a/custom_components/pura/binary_sensor.py b/custom_components/pura/binary_sensor.py index a5f00ac..5181849 100644 --- a/custom_components/pura/binary_sensor.py +++ b/custom_components/pura/binary_sensor.py @@ -60,7 +60,7 @@ async def async_setup_entry( config_entry: ConfigEntry, async_add_entities: AddEntitiesCallback, ) -> None: - """Set up Pura binary_sensors using config entry.""" + """Set up Pura binary sensors using config entry.""" coordinator: PuraDataUpdateCoordinator = hass.data[DOMAIN][config_entry.entry_id] entities = [ diff --git a/custom_components/pura/sensor.py b/custom_components/pura/sensor.py index 99fd8e8..1c711ce 100644 --- a/custom_components/pura/sensor.py +++ b/custom_components/pura/sensor.py @@ -30,7 +30,7 @@ class RequiredKeysMixin: """Required keys mixin.""" - value_fn: Callable[[dict], Any] + value_fn: Callable[[dict], Any | None] @dataclass @@ -56,7 +56,7 @@ class PuraSensorEntityDescription(SensorEntityDescription, RequiredKeysMixin): entity_category=EntityCategory.DIAGNOSTIC, icon="mdi:scent", available_fn=lambda data: has_fragrance(data, 1), - value_fn=lambda data: data["bay_1"]["name"], + value_fn=lambda data: data["bay_1"].get("name"), ), PuraSensorEntityDescription( key="intensity",