Skip to content

Commit

Permalink
Fix for car diffusers without fragrance name in data and allow deleti…
Browse files Browse the repository at this point in the history
…ng devices if no longer present
  • Loading branch information
natekspencer committed Dec 2, 2023
1 parent 61fe9ba commit 4a0d86f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
17 changes: 17 additions & 0 deletions custom_components/pura/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
)
2 changes: 1 addition & 1 deletion custom_components/pura/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
4 changes: 2 additions & 2 deletions custom_components/pura/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
class RequiredKeysMixin:
"""Required keys mixin."""

value_fn: Callable[[dict], Any]
value_fn: Callable[[dict], Any | None]


@dataclass
Expand All @@ -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",
Expand Down

0 comments on commit 4a0d86f

Please sign in to comment.