Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ConfigEntry runtime_data in detailed hello world #70

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading