Skip to content

Commit

Permalink
thermopro: add set_datetime service for TP358/TP393
Browse files Browse the repository at this point in the history
  • Loading branch information
stephan48 committed Feb 4, 2025
1 parent 6b32587 commit 70bb009
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 4 deletions.
9 changes: 8 additions & 1 deletion homeassistant/components/thermopro/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,20 @@
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers import config_validation as cv

from .const import DOMAIN

PLATFORMS: list[Platform] = [Platform.SENSOR]
CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN)

PLATFORMS: list[Platform] = [Platform.BUTTON, Platform.SENSOR]

_LOGGER = logging.getLogger(__name__)


SERVICE_NAME = "set_datetime"


async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up ThermoPro BLE device from a config entry."""
address = entry.unique_id
Expand All @@ -39,6 +45,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
entry.async_on_unload(
coordinator.async_start()
) # only start after all platforms have had a chance to subscribe

return True


Expand Down
98 changes: 98 additions & 0 deletions homeassistant/components/thermopro/button.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
"""Demo platform that offers a fake button entity."""

from __future__ import annotations

import logging

from thermopro_ble import ThermoProDevice

from homeassistant.components import persistent_notification
from homeassistant.components.bluetooth import async_ble_device_from_address
from homeassistant.components.button import ButtonEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.util.dt import now

from . import DOMAIN

_LOGGER = logging.getLogger(__name__)


async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the demo button platform."""

assert config_entry.unique_id is not None

device_registry = dr.async_get(hass)
device = device_registry.async_get_device(
connections={(dr.CONNECTION_BLUETOOTH, config_entry.unique_id)},
identifiers={(dr.CONNECTION_BLUETOOTH, config_entry.unique_id)},
)

assert device is not None

_LOGGER.debug("UHUHUHUH %s - %s - %s", config_entry, config_entry.data, device)

if device.model not in ("TP358", "TP393"):
return

async_add_entities(
[
ThermoProButton(
address=config_entry.unique_id,
unique_id="push",
device_name="Push",
),
]
)


class ThermoProButton(ButtonEntity):
"""Representation of a demo button entity."""

_attr_has_entity_name = True
_attr_name = None
_attr_should_poll = False

def __init__(
self,
address: str,
unique_id: str,
device_name: str,
) -> None:
"""Initialize the Demo button entity."""
self.address = address
self._attr_unique_id = unique_id
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, unique_id)},
name=device_name,
)

async def async_press(self) -> None:
"""Send out a persistent notification."""
address = self.address

assert address is not None

dt = now()

persistent_notification.async_create(
self.hass, f"Button pressed {address} {dt}", title="Button"
)

ble = async_ble_device_from_address(self.hass, address, connectable=True)

assert ble is not None

tpd = ThermoProDevice(ble)

await tpd.set_datetime(dt, False)

self.hass.bus.async_fire("demo_button_pressed")
7 changes: 7 additions & 0 deletions homeassistant/components/thermopro/icons.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"services": {
"set_datetime": {
"service": "mdi:clock"
}
}
}
2 changes: 1 addition & 1 deletion homeassistant/components/thermopro/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@
"dependencies": ["bluetooth_adapters"],
"documentation": "https://www.home-assistant.io/integrations/thermopro",
"iot_class": "local_push",
"requirements": ["thermopro-ble==0.10.1"]
"requirements": ["thermopro-ble==0.11.0"]
}
2 changes: 1 addition & 1 deletion requirements_all.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion requirements_test_all.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 70bb009

Please sign in to comment.