Skip to content

Commit

Permalink
✨ import prototype of the king_smith custom integration
Browse files Browse the repository at this point in the history
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
madmatah committed Mar 28, 2024
1 parent 8a6d406 commit f511f63
Show file tree
Hide file tree
Showing 14 changed files with 583 additions and 400 deletions.
12 changes: 11 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,19 @@
All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic
and this project adheres to [Semantic
Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Added

- 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

[unreleased]: https://github.com/madmatah/hass-walkingpad
75 changes: 45 additions & 30 deletions custom_components/king_smith/__init__.py
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
90 changes: 0 additions & 90 deletions custom_components/king_smith/api.py

This file was deleted.

50 changes: 0 additions & 50 deletions custom_components/king_smith/binary_sensor.py

This file was deleted.

Loading

0 comments on commit f511f63

Please sign in to comment.