Control datetime on SwitchBot Meter Pro CO2 (#161808)

This commit is contained in:
elgris
2026-02-19 00:32:23 +01:00
committed by GitHub
parent e9be363f29
commit ca4d537529
4 changed files with 163 additions and 2 deletions

View File

@@ -53,7 +53,11 @@ PLATFORMS_BY_TYPE = {
Platform.SENSOR,
],
SupportedModels.HYGROMETER.value: [Platform.SENSOR],
SupportedModels.HYGROMETER_CO2.value: [Platform.SENSOR, Platform.SELECT],
SupportedModels.HYGROMETER_CO2.value: [
Platform.BUTTON,
Platform.SENSOR,
Platform.SELECT,
],
SupportedModels.CONTACT.value: [Platform.BINARY_SENSOR, Platform.SENSOR],
SupportedModels.MOTION.value: [Platform.BINARY_SENSOR, Platform.SENSOR],
SupportedModels.PRESENCE_SENSOR.value: [Platform.BINARY_SENSOR, Platform.SENSOR],

View File

@@ -5,8 +5,10 @@ import logging
import switchbot
from homeassistant.components.button import ButtonEntity
from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from homeassistant.util import dt as dt_util
from .coordinator import SwitchbotConfigEntry, SwitchbotDataUpdateCoordinator
from .entity import SwitchbotEntity, exception_handler
@@ -31,6 +33,9 @@ async def async_setup_entry(
]
)
if isinstance(coordinator.device, switchbot.SwitchbotMeterProCO2):
async_add_entities([SwitchBotMeterProCO2SyncDateTimeButton(coordinator)])
class SwitchBotArtFrameButtonBase(SwitchbotEntity, ButtonEntity):
"""Base class for Art Frame buttons."""
@@ -64,3 +69,45 @@ class SwitchBotArtFramePrevButton(SwitchBotArtFrameButtonBase):
"""Handle the button press."""
_LOGGER.debug("Pressing previous image button %s", self._address)
await self._device.prev_image()
class SwitchBotMeterProCO2SyncDateTimeButton(SwitchbotEntity, ButtonEntity):
"""Button to sync date and time on Meter Pro CO2 to the current HA instance datetime."""
_device: switchbot.SwitchbotMeterProCO2
_attr_entity_category = EntityCategory.CONFIG
_attr_translation_key = "sync_datetime"
def __init__(self, coordinator: SwitchbotDataUpdateCoordinator) -> None:
"""Initialize the sync time button."""
super().__init__(coordinator)
self._attr_unique_id = f"{coordinator.base_unique_id}_sync_datetime"
@exception_handler
async def async_press(self) -> None:
"""Sync time with Home Assistant."""
now = dt_util.now()
# Get UTC offset components
utc_offset = now.utcoffset()
utc_offset_hours, utc_offset_minutes = 0, 0
if utc_offset is not None:
total_seconds = int(utc_offset.total_seconds())
utc_offset_hours = total_seconds // 3600
utc_offset_minutes = abs(total_seconds % 3600) // 60
timestamp = int(now.timestamp())
_LOGGER.debug(
"Syncing time for %s: timestamp=%s, utc_offset_hours=%s, utc_offset_minutes=%s",
self._address,
timestamp,
utc_offset_hours,
utc_offset_minutes,
)
await self._device.set_datetime(
timestamp=timestamp,
utc_offset_hours=utc_offset_hours,
utc_offset_minutes=utc_offset_minutes,
)

View File

@@ -106,6 +106,9 @@
},
"previous_image": {
"name": "Previous image"
},
"sync_datetime": {
"name": "Sync date and time"
}
},
"climate": {

View File

@@ -1,6 +1,7 @@
"""Tests for the switchbot button platform."""
from collections.abc import Callable
from datetime import UTC, datetime, timedelta, timezone
from unittest.mock import AsyncMock, patch
import pytest
@@ -8,8 +9,9 @@ import pytest
from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN, SERVICE_PRESS
from homeassistant.const import ATTR_ENTITY_ID
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
from . import ART_FRAME_INFO
from . import ART_FRAME_INFO, DOMAIN, WOMETERTHPC_SERVICE_INFO
from tests.common import MockConfigEntry
from tests.components.bluetooth import inject_bluetooth_service_info
@@ -60,3 +62,108 @@ async def test_art_frame_button_press(
)
mocked_instance.assert_awaited_once()
async def test_meter_pro_co2_sync_datetime_button(
hass: HomeAssistant,
mock_entry_factory: Callable[[str], MockConfigEntry],
) -> None:
"""Test pressing the sync datetime button on Meter Pro CO2."""
await async_setup_component(hass, DOMAIN, {})
inject_bluetooth_service_info(hass, WOMETERTHPC_SERVICE_INFO)
entry = mock_entry_factory("hygrometer_co2")
entry.add_to_hass(hass)
mock_set_datetime = AsyncMock(return_value=True)
# Use a fixed datetime for testing
fixed_time = datetime(2025, 1, 9, 12, 30, 45, tzinfo=UTC)
with (
patch(
"switchbot.SwitchbotMeterProCO2.set_datetime",
mock_set_datetime,
),
patch(
"homeassistant.components.switchbot.button.dt_util.now",
return_value=fixed_time,
),
):
assert await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
entity_ids = [
entity.entity_id for entity in hass.states.async_all(BUTTON_DOMAIN)
]
assert "button.test_name_sync_date_and_time" in entity_ids
await hass.services.async_call(
BUTTON_DOMAIN,
SERVICE_PRESS,
{ATTR_ENTITY_ID: "button.test_name_sync_date_and_time"},
blocking=True,
)
mock_set_datetime.assert_awaited_once_with(
timestamp=int(fixed_time.timestamp()),
utc_offset_hours=0,
utc_offset_minutes=0,
)
@pytest.mark.parametrize(
("tz", "expected_utc_offset_hours", "expected_utc_offset_minutes"),
[
(timezone(timedelta(hours=0, minutes=0)), 0, 0),
(timezone(timedelta(hours=0, minutes=30)), 0, 30),
(timezone(timedelta(hours=8, minutes=0)), 8, 0),
(timezone(timedelta(hours=-5, minutes=30)), -5, 30),
(timezone(timedelta(hours=5, minutes=30)), 5, 30),
(timezone(timedelta(hours=-5, minutes=-30)), -6, 30), # -6h + 30m = -5:30
(timezone(timedelta(hours=-5, minutes=-45)), -6, 15), # -6h + 15m = -5:45
],
)
async def test_meter_pro_co2_sync_datetime_button_with_timezone(
hass: HomeAssistant,
mock_entry_factory: Callable[[str], MockConfigEntry],
tz: timezone,
expected_utc_offset_hours: int,
expected_utc_offset_minutes: int,
) -> None:
"""Test sync datetime button with non-UTC timezone."""
await async_setup_component(hass, DOMAIN, {})
inject_bluetooth_service_info(hass, WOMETERTHPC_SERVICE_INFO)
entry = mock_entry_factory("hygrometer_co2")
entry.add_to_hass(hass)
mock_set_datetime = AsyncMock(return_value=True)
fixed_time = datetime(2025, 1, 9, 18, 0, 45, tzinfo=tz)
with (
patch(
"switchbot.SwitchbotMeterProCO2.set_datetime",
mock_set_datetime,
),
patch(
"homeassistant.components.switchbot.button.dt_util.now",
return_value=fixed_time,
),
):
assert await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
await hass.services.async_call(
BUTTON_DOMAIN,
SERVICE_PRESS,
{ATTR_ENTITY_ID: "button.test_name_sync_date_and_time"},
blocking=True,
)
mock_set_datetime.assert_awaited_once_with(
timestamp=int(fixed_time.timestamp()),
utc_offset_hours=expected_utc_offset_hours,
utc_offset_minutes=expected_utc_offset_minutes,
)