generated from ludeeus/integration_blueprint
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ import prototype of the king_smith custom integration
A prototype of the walkingpad custom integration was created with the following features: - support for WalkingPad A1 Pro - manual config flow - bluetooth discovery config flow - distance sensor - steps sensor - duration sensors (available in several units : minutes, hours, days) - current speed sensor This commit integrates the prototype into the project.
- Loading branch information
Showing
14 changed files
with
583 additions
and
400 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,66 @@ | ||
"""Custom integration to integrate king_smith with Home Assistant.""" | ||
"""The walkingpad integration.""" | ||
from __future__ import annotations | ||
|
||
from typing import TypedDict | ||
|
||
from homeassistant.components import bluetooth | ||
from homeassistant.config_entries import ConfigEntry | ||
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, Platform | ||
from homeassistant.const import CONF_MAC, CONF_NAME, Platform | ||
from homeassistant.core import HomeAssistant | ||
from homeassistant.helpers.aiohttp_client import async_get_clientsession | ||
from homeassistant.exceptions import ConfigEntryNotReady | ||
|
||
from .api import IntegrationBlueprintApiClient | ||
from .const import DOMAIN | ||
from .coordinator import BlueprintDataUpdateCoordinator | ||
from .coordinator import WalkingPadCoordinator | ||
from .walkingpad import WalkingPad | ||
|
||
PLATFORMS: list[Platform] = [Platform.SENSOR] | ||
|
||
|
||
PLATFORMS: list[Platform] = [ | ||
Platform.SENSOR, | ||
Platform.BINARY_SENSOR, | ||
Platform.SWITCH, | ||
] | ||
class WalkingPadIntegrationData(TypedDict): | ||
"""A type to represent the data stored by the integration for each entity.""" | ||
|
||
device: WalkingPad | ||
coordinator: WalkingPadCoordinator | ||
|
||
|
||
# https://developers.home-assistant.io/docs/config_entries_index/#setting-up-an-entry | ||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: | ||
"""Set up this integration using UI.""" | ||
"""Set up walkingpad from a config entry.""" | ||
|
||
hass.data.setdefault(DOMAIN, {}) | ||
hass.data[DOMAIN][entry.entry_id] = coordinator = BlueprintDataUpdateCoordinator( | ||
hass=hass, | ||
client=IntegrationBlueprintApiClient( | ||
username=entry.data[CONF_USERNAME], | ||
password=entry.data[CONF_PASSWORD], | ||
session=async_get_clientsession(hass), | ||
), | ||
address = entry.data.get(CONF_MAC) | ||
|
||
ble_device = bluetooth.async_ble_device_from_address( | ||
hass, entry.data.get(CONF_MAC), connectable=True | ||
) | ||
# https://developers.home-assistant.io/docs/integration_fetching_data#coordinated-single-api-poll-for-data-for-all-entities | ||
await coordinator.async_config_entry_first_refresh() | ||
if ble_device is None: | ||
# Check if any HA scanner on: | ||
count_scanners = bluetooth.async_scanner_count(hass, connectable=True) | ||
if count_scanners < 1: | ||
raise ConfigEntryNotReady( | ||
"No bluetooth scanner detected. \ | ||
Enable the bluetooth integration or ensure an esphome device \ | ||
is running as a bluetooth proxy" | ||
) | ||
raise ConfigEntryNotReady(f"Could not find Walkingpad with address {address}") | ||
|
||
name = entry.data.get(CONF_NAME) or DOMAIN | ||
walkingpad_device = WalkingPad(name, ble_device) | ||
coordinator = WalkingPadCoordinator(hass, walkingpad_device) | ||
|
||
integration_data: WalkingPadIntegrationData = { | ||
"device": walkingpad_device, | ||
"coordinator": coordinator, | ||
} | ||
hass.data[DOMAIN][entry.entry_id] = integration_data | ||
|
||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) | ||
entry.async_on_unload(entry.add_update_listener(async_reload_entry)) | ||
|
||
return True | ||
|
||
|
||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: | ||
"""Handle removal of an entry.""" | ||
if unloaded := await hass.config_entries.async_unload_platforms(entry, PLATFORMS): | ||
"""Unload a config entry.""" | ||
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS): | ||
hass.data[DOMAIN].pop(entry.entry_id) | ||
return unloaded | ||
|
||
|
||
async def async_reload_entry(hass: HomeAssistant, entry: ConfigEntry) -> None: | ||
"""Reload config entry.""" | ||
await async_unload_entry(hass, entry) | ||
await async_setup_entry(hass, entry) | ||
return unload_ok |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.