Skip to content

Commit

Permalink
Merge pull request #668 from plugwise/coordinator-use
Browse files Browse the repository at this point in the history
Implement various new Core features
  • Loading branch information
bouwew committed Jun 20, 2024
2 parents cd2b78a + 7797635 commit 46e9d62
Show file tree
Hide file tree
Showing 13 changed files with 55 additions and 81 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

## Versions from 0.40 and up

## v051.3
## Ongoing

- Implement various new Core features.

## v0.51.3

- Implement fix for Core Issue #119686 via plugwise [v0.38.3](https://github.com/plugwise/python-plugwise/releases/tag/v0.38.3)

Expand Down
24 changes: 9 additions & 15 deletions custom_components/plugwise/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@

from .const import (
CONF_REFRESH_INTERVAL, # pw-beta options
COORDINATOR,
DOMAIN,
LOGGER,
PLATFORMS,
SERVICE_DELETE,
UNDO_UPDATE_LISTENER,
)
from .coordinator import PlugwiseDataUpdateCoordinator

type PlugwiseConfigEntry = ConfigEntry[PlugwiseDataUpdateCoordinator]

async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:

async def async_setup_entry(hass: HomeAssistant, entry: PlugwiseConfigEntry) -> bool:
"""Set up the Plugwise Device from a config entry."""
await er.async_migrate_entries(hass, entry.entry_id, async_migrate_entity_entry)

Expand All @@ -41,12 +41,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
# Migrate a changed sensor unique_id
migrate_sensor_entities(hass, coordinator)

undo_listener = entry.add_update_listener(_update_listener) # pw-beta

hass.data.setdefault(DOMAIN, {})[entry.entry_id] = {
COORDINATOR: coordinator, # pw-beta
UNDO_UPDATE_LISTENER: undo_listener, # pw-beta
}
entry.runtime_data = coordinator # pw-beta

device_registry = dr.async_get(hass)
device_registry.async_get_or_create(
Expand Down Expand Up @@ -76,6 +71,7 @@ async def delete_notification(
)

await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
entry.async_on_unload(entry.add_update_listener(update_listener))

for component in PLATFORMS: # pw-beta
if component == Platform.BINARY_SENSOR:
Expand All @@ -85,17 +81,15 @@ async def delete_notification(

return True

async def _update_listener(
hass: HomeAssistant, entry: ConfigEntry
async def update_listener(
hass: HomeAssistant, entry: PlugwiseConfigEntry
) -> None: # pragma: no cover # pw-beta
"""Handle options update."""
await hass.config_entries.async_reload(entry.entry_id)

async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_unload_entry(hass: HomeAssistant, entry: PlugwiseConfigEntry) -> bool:
"""Unload a config entry."""
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
hass.data[DOMAIN].pop(entry.entry_id)
return unload_ok
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)

@callback
def async_migrate_entity_entry(entry: er.RegistryEntry) -> dict[str, Any] | None:
Expand Down
8 changes: 4 additions & 4 deletions custom_components/plugwise/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
BinarySensorEntity,
BinarySensorEntityDescription,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_NAME, EntityCategory
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from . import PlugwiseConfigEntry
from .const import (
BINARY_SENSORS,
COMPRESSOR_STATE,
Expand All @@ -32,7 +32,7 @@
SEVERITIES,
)
from .coordinator import PlugwiseDataUpdateCoordinator
from .entity import PlugwiseEntity, get_coordinator
from .entity import PlugwiseEntity

PARALLEL_UPDATES = 0

Expand Down Expand Up @@ -90,11 +90,11 @@ class PlugwiseBinarySensorEntityDescription(BinarySensorEntityDescription):

async def async_setup_entry(
hass: HomeAssistant,
entry: ConfigEntry,
entry: PlugwiseConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the Plugwise binary_sensors from a ConfigEntry."""
coordinator = get_coordinator(hass, entry.entry_id)
coordinator = entry.runtime_data

@callback
def _add_entities() -> None:
Expand Down
8 changes: 4 additions & 4 deletions custom_components/plugwise/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
ButtonEntity,
ButtonEntityDescription,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_NAME, EntityCategory
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from . import PlugwiseConfigEntry
from .const import GATEWAY_ID, LOGGER, REBOOT
from .coordinator import PlugwiseDataUpdateCoordinator
from .entity import PlugwiseEntity, get_coordinator
from .entity import PlugwiseEntity
from .util import plugwise_command

BUTTON_TYPES: tuple[ButtonEntityDescription, ...] = (
Expand All @@ -28,11 +28,11 @@

async def async_setup_entry(
hass: HomeAssistant,
entry: ConfigEntry,
entry: PlugwiseConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the Plugwise buttons from a ConfigEntry."""
coordinator = get_coordinator(hass, entry.entry_id)
coordinator = entry.runtime_data

@callback
def _add_entities() -> None:
Expand Down
8 changes: 4 additions & 4 deletions custom_components/plugwise/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
HVACAction,
HVACMode,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_TEMPERATURE, STATE_OFF, STATE_ON, UnitOfTemperature
from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from . import PlugwiseConfigEntry
from .const import (
ACTIVE_PRESET,
AVAILABLE_SCHEDULES,
Expand Down Expand Up @@ -48,17 +48,17 @@
UPPER_BOUND,
)
from .coordinator import PlugwiseDataUpdateCoordinator
from .entity import PlugwiseEntity, get_coordinator
from .entity import PlugwiseEntity
from .util import plugwise_command


async def async_setup_entry(
hass: HomeAssistant,
entry: ConfigEntry,
entry: PlugwiseConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the Smile Thermostats from a ConfigEntry."""
coordinator = get_coordinator(hass, entry.entry_id)
coordinator = entry.runtime_data
homekit_enabled: bool = entry.options.get(
CONF_HOMEKIT_EMULATION, False
) # pw-beta homekit emulation
Expand Down
23 changes: 10 additions & 13 deletions custom_components/plugwise/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
)
import voluptuous as vol

from homeassistant import config_entries
from homeassistant.components.zeroconf import ZeroconfServiceInfo
from homeassistant.config_entries import (
SOURCE_USER,
ConfigEntry,
ConfigFlow,
ConfigFlowResult,
OptionsFlow,
OptionsFlowWithConfigEntry,
)
from homeassistant.const import (
ATTR_CONFIGURATION_URL,
Expand Down Expand Up @@ -140,7 +141,7 @@ async def async_step_zeroconf(
CONF_PASSWORD: config_entry.data[CONF_PASSWORD],
},
)
except Exception: # pylint: disable=broad-except
except Exception: # noqa: BLE001
self._abort_if_unique_id_configured()
else:
self._abort_if_unique_id_configured(
Expand Down Expand Up @@ -227,7 +228,7 @@ async def async_step_user(
errors[CONF_BASE] = "response_error"
except UnsupportedDeviceError:
errors[CONF_BASE] = "unsupported"
except Exception: # pylint: disable=broad-except
except Exception: # noqa: BLE001
errors[CONF_BASE] = "unknown"

if errors:
Expand All @@ -248,28 +249,24 @@ async def async_step_user(
@callback
def async_get_options_flow(
config_entry: ConfigEntry,
) -> config_entries.OptionsFlow: # pw-beta options
) -> OptionsFlow: # pw-beta options
"""Get the options flow for this handler."""
return PlugwiseOptionsFlowHandler(config_entry)


# pw-beta - change the scan-interval via CONFIGURE
# pw-beta - add homekit emulation via CONFIGURE
# pw-beta - change the frontend refresh interval via CONFIGURE
class PlugwiseOptionsFlowHandler(config_entries.OptionsFlow): # pw-beta options
class PlugwiseOptionsFlowHandler(OptionsFlowWithConfigEntry): # pw-beta options
"""Plugwise option flow."""

def __init__(self, config_entry: ConfigEntry) -> None: # pragma: no cover
"""Initialize options flow."""
self.config_entry = config_entry

async def async_step_none(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult: # pragma: no cover
"""No options available."""
if user_input is not None:
# Apparently not possible to abort an options flow at the moment
return self.async_create_entry(title="", data=self.config_entry.options)
return self.async_create_entry(title="", data=self._options)

return self.async_show_form(step_id="none")

Expand All @@ -291,7 +288,7 @@ async def async_step_init(
data = {
vol.Optional(
CONF_SCAN_INTERVAL,
default=self.config_entry.options.get(
default=self._options.get(
CONF_SCAN_INTERVAL, interval.seconds
),
): vol.All(cv.positive_int, vol.Clamp(min=10)),
Expand All @@ -304,13 +301,13 @@ async def async_step_init(
{
vol.Optional(
CONF_HOMEKIT_EMULATION,
default=self.config_entry.options.get(
default=self._options.get(
CONF_HOMEKIT_EMULATION, False
),
): cv.boolean,
vol.Optional(
CONF_REFRESH_INTERVAL,
default=self.config_entry.options.get(CONF_REFRESH_INTERVAL, 1.5),
default=self._options.get(CONF_REFRESH_INTERVAL, 1.5),
): vol.All(vol.Coerce(float), vol.Range(min=1.5, max=10.0)),
}
) # pw-beta
Expand Down
1 change: 0 additions & 1 deletion custom_components/plugwise/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
SMILE: Final = "smile"
STRETCH: Final = "stretch"
STRETCH_USERNAME: Final = "stretch"
UNDO_UPDATE_LISTENER: Final = "undo_update_listener"
UNIQUE_IDS: Final = "unique_ids"
ZIGBEE_MAC_ADDRESS: Final = "zigbee_mac_address"

Expand Down
13 changes: 3 additions & 10 deletions custom_components/plugwise/diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,16 @@

from typing import Any

from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant

from .const import (
COORDINATOR, # pw-beta
DOMAIN,
)
from .coordinator import PlugwiseDataUpdateCoordinator
from . import PlugwiseConfigEntry


async def async_get_config_entry_diagnostics(
hass: HomeAssistant, entry: ConfigEntry
hass: HomeAssistant, entry: PlugwiseConfigEntry
) -> dict[str, Any]:
"""Return diagnostics for a config entry."""
coordinator: PlugwiseDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id][
COORDINATOR
]
coordinator = entry.runtime_data
return {
"gateway": coordinator.data.gateway,
"devices": coordinator.data.devices,
Expand Down
13 changes: 0 additions & 13 deletions custom_components/plugwise/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from plugwise.constants import DeviceData

from homeassistant.const import ATTR_NAME, ATTR_VIA_DEVICE, CONF_HOST
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import (
CONNECTION_NETWORK_MAC,
CONNECTION_ZIGBEE,
Expand All @@ -14,7 +13,6 @@

from .const import (
AVAILABLE,
COORDINATOR,
DOMAIN,
FIRMWARE,
GATEWAY_ID,
Expand All @@ -28,17 +26,6 @@
from .coordinator import PlugwiseDataUpdateCoordinator


def get_coordinator(
hass: HomeAssistant, config_entry_id: str
) -> PlugwiseDataUpdateCoordinator:
"""Get coordinator for given config entry id."""
coordinator: PlugwiseDataUpdateCoordinator = hass.data[DOMAIN][config_entry_id][
COORDINATOR
]

return coordinator


class PlugwiseEntity(CoordinatorEntity[PlugwiseDataUpdateCoordinator]):
"""Represent a PlugWise Entity."""

Expand Down
8 changes: 4 additions & 4 deletions custom_components/plugwise/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
NumberEntityDescription,
NumberMode,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_NAME, EntityCategory, UnitOfTemperature
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from . import PlugwiseConfigEntry
from .const import (
LOGGER,
LOWER_BOUND,
Expand All @@ -25,7 +25,7 @@
NumberType,
)
from .coordinator import PlugwiseDataUpdateCoordinator
from .entity import PlugwiseEntity, get_coordinator
from .entity import PlugwiseEntity
from .util import plugwise_command


Expand Down Expand Up @@ -63,11 +63,11 @@ class PlugwiseNumberEntityDescription(NumberEntityDescription):

async def async_setup_entry(
hass: HomeAssistant,
entry: ConfigEntry,
entry: PlugwiseConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the Plugwise numbers from a ConfigEntry."""
coordinator = get_coordinator(hass, entry.entry_id)
coordinator = entry.runtime_data

@callback
def _add_entities() -> None:
Expand Down
Loading

0 comments on commit 46e9d62

Please sign in to comment.