Skip to content

Commit

Permalink
Use ConfigEntry runtime_data in detailed hello world (#70)
Browse files Browse the repository at this point in the history
* refactor(detailed_hello_world): use custom ConfigEntry runtime_data

* refactor(detailed_hello_world): add type annotations to sensor setup
  • Loading branch information
HipsterBrown authored Aug 20, 2024
1 parent 8cbb703 commit 54d666d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
10 changes: 5 additions & 5 deletions custom_components/detailed_hello_world_push/__init__.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
"""The Detailed Hello World Push integration."""

from __future__ import annotations

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

from . import hub
from .const import DOMAIN

# List of platforms to support. There should be a matching .py file for each,
# eg <cover.py> and <sensor.py>
PLATFORMS = [Platform.SENSOR, Platform.COVER]

type HubConfigEntry = ConfigEntry[hub.Hub]


async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_setup_entry(hass: HomeAssistant, entry: HubConfigEntry) -> bool:
"""Set up Hello World from a config entry."""
# Store an instance of the "connecting" class that does the work of speaking
# with your actual devices.
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = hub.Hub(hass, entry.data["host"])
entry.runtime_data = hub.Hub(hass, entry.data["host"])

# This creates each HA object for each platform your device requires.
# It's done by calling the `async_setup_entry` function in each platform module.
Expand All @@ -31,7 +33,5 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
# needs to unload itself, and remove callbacks. See the classes for further
# details
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
if unload_ok:
hass.data[DOMAIN].pop(entry.entry_id)

return unload_ok
8 changes: 4 additions & 4 deletions custom_components/detailed_hello_world_push/cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,24 @@
CoverEntityFeature,
CoverEntity,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from . import HubConfigEntry
from .const import DOMAIN


# This function is called as part of the __init__.async_setup_entry (via the
# hass.config_entries.async_forward_entry_setup call)
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
config_entry: HubConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Add cover for passed config_entry in HA."""
# The hub is loaded from the associated hass.data entry that was created in the
# The hub is loaded from the associated entry runtime data that was set in the
# __init__.async_setup_entry function
hub = hass.data[DOMAIN][config_entry.entry_id]
hub = config_entry.runtime_data

# Add all entities to HA
async_add_entities(HelloWorldCover(roller) for roller in hub.rollers)
Expand Down
15 changes: 9 additions & 6 deletions custom_components/detailed_hello_world_push/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,30 @@

from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity,
SensorStateClass,
)
from homeassistant.const import (
ATTR_VOLTAGE,
PERCENTAGE,
LIGHT_LUX,
ATTR_UNIT_OF_MEASUREMENT,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from . import HubConfigEntry
from .const import DOMAIN


# See cover.py for more details.
# Note how both entities for each roller sensor (battry and illuminance) are added at
# the same time to the same list. This way only a single async_add_devices call is
# required.
async def async_setup_entry(hass, config_entry, async_add_entities):
async def async_setup_entry(
hass: HomeAssistant,
config_entry: HubConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Add sensors for passed config_entry in HA."""
hub = hass.data[DOMAIN][config_entry.entry_id]
hub = config_entry.runtime_data

new_devices = []
for roller in hub.rollers:
Expand Down

0 comments on commit 54d666d

Please sign in to comment.