Skip to content

Commit

Permalink
Use pybalboa 1.0.0 (#87214)
Browse files Browse the repository at this point in the history
* Use pybalboa 1.0.0

* Code changes per PR review
  • Loading branch information
natekspencer authored Feb 5, 2023
1 parent 899342d commit 11ccd16
Show file tree
Hide file tree
Showing 19 changed files with 495 additions and 562 deletions.
1 change: 0 additions & 1 deletion .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ omit =
homeassistant/components/baf/sensor.py
homeassistant/components/baf/switch.py
homeassistant/components/baidu/tts.py
homeassistant/components/balboa/__init__.py
homeassistant/components/bbox/device_tracker.py
homeassistant/components/bbox/sensor.py
homeassistant/components/beewi_smartclim/sensor.py
Expand Down
4 changes: 2 additions & 2 deletions CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ build.json @home-assistant/supervisor
/tests/components/backup/ @home-assistant/core
/homeassistant/components/baf/ @bdraco @jfroy
/tests/components/baf/ @bdraco @jfroy
/homeassistant/components/balboa/ @garbled1
/tests/components/balboa/ @garbled1
/homeassistant/components/balboa/ @garbled1 @natekspencer
/tests/components/balboa/ @garbled1 @natekspencer
/homeassistant/components/bayesian/ @HarvsG
/tests/components/bayesian/ @HarvsG
/homeassistant/components/beewi_smartclim/ @alemuro
Expand Down
78 changes: 24 additions & 54 deletions homeassistant/components/balboa/__init__.py
Original file line number Diff line number Diff line change
@@ -1,78 +1,46 @@
"""The Balboa Spa Client integration."""
import asyncio
from __future__ import annotations

from datetime import datetime, timedelta
import time
import logging

from pybalboa import BalboaSpaWifi
from pybalboa import SpaClient

from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_HOST
from homeassistant.const import CONF_HOST, Platform
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers.dispatcher import async_dispatcher_send
from homeassistant.helpers.event import async_track_time_interval
import homeassistant.util.dt as dt_util

from .const import (
_LOGGER,
CONF_SYNC_TIME,
DEFAULT_SYNC_TIME,
DOMAIN,
PLATFORMS,
SIGNAL_UPDATE,
)
from .const import CONF_SYNC_TIME, DEFAULT_SYNC_TIME, DOMAIN

_LOGGER = logging.getLogger(__name__)

PLATFORMS = [Platform.BINARY_SENSOR, Platform.CLIMATE]


KEEP_ALIVE_INTERVAL = timedelta(minutes=1)
SYNC_TIME_INTERVAL = timedelta(days=1)
SYNC_TIME_INTERVAL = timedelta(hours=1)


async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up Balboa Spa from a config entry."""
host = entry.data[CONF_HOST]

_LOGGER.debug("Attempting to connect to %s", host)
spa = BalboaSpaWifi(host)
connected = await spa.connect()
if not connected:
spa = SpaClient(host)
if not await spa.connect():
_LOGGER.error("Failed to connect to spa at %s", host)
raise ConfigEntryNotReady
raise ConfigEntryNotReady("Unable to connect")
if not await spa.async_configuration_loaded():
_LOGGER.error("Failed to get spa info at %s", host)
raise ConfigEntryNotReady("Unable to configure")

hass.data.setdefault(DOMAIN, {})[entry.entry_id] = spa

async def _async_balboa_update_cb() -> None:
"""Primary update callback called from pybalboa."""
_LOGGER.debug("Primary update callback triggered")
async_dispatcher_send(hass, SIGNAL_UPDATE.format(entry.entry_id))

# set the callback so we know we have new data
spa.new_data_cb = _async_balboa_update_cb

_LOGGER.debug("Starting listener and monitor tasks")
monitoring_tasks = [asyncio.create_task(spa.listen())]
await spa.spa_configured()
monitoring_tasks.append(asyncio.create_task(spa.check_connection_status()))

def stop_monitoring() -> None:
"""Stop monitoring the spa connection."""
_LOGGER.debug("Canceling listener and monitor tasks")
for task in monitoring_tasks:
task.cancel()

entry.async_on_unload(stop_monitoring)

# At this point we have a configured spa.
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)

async def keep_alive(now: datetime) -> None:
"""Keep alive task."""
_LOGGER.debug("Keep alive")
await spa.send_mod_ident_req()

entry.async_on_unload(
async_track_time_interval(hass, keep_alive, KEEP_ALIVE_INTERVAL)
)

# call update_listener on startup and for options change as well.
await async_setup_time_sync(hass, entry)
entry.async_on_unload(entry.add_update_listener(update_listener))

Expand All @@ -82,7 +50,7 @@ async def keep_alive(now: datetime) -> None:
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry."""
_LOGGER.debug("Disconnecting from spa")
spa: BalboaSpaWifi = hass.data[DOMAIN][entry.entry_id]
spa: SpaClient = hass.data[DOMAIN][entry.entry_id]

if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
hass.data[DOMAIN].pop(entry.entry_id)
Expand All @@ -103,11 +71,13 @@ async def async_setup_time_sync(hass: HomeAssistant, entry: ConfigEntry) -> None
return

_LOGGER.debug("Setting up daily time sync")
spa: BalboaSpaWifi = hass.data[DOMAIN][entry.entry_id]
spa: SpaClient = hass.data[DOMAIN][entry.entry_id]

async def sync_time(now: datetime) -> None:
_LOGGER.debug("Syncing time with Home Assistant")
await spa.set_time(time.strptime(str(dt_util.now()), "%Y-%m-%d %H:%M:%S.%f%z"))
now = dt_util.as_local(now)
if (now.hour, now.minute) != (spa.time_hour, spa.time_minute):
_LOGGER.debug("Syncing time with Home Assistant")
await spa.set_time(now.hour, now.minute)

await sync_time(dt_util.utcnow())
entry.async_on_unload(
Expand Down
104 changes: 67 additions & 37 deletions homeassistant/components/balboa/binary_sensor.py
Original file line number Diff line number Diff line change
@@ -1,68 +1,98 @@
"""Support for Balboa Spa binary sensors."""
from __future__ import annotations

from collections.abc import Callable
from dataclasses import dataclass

from pybalboa import SpaClient

from homeassistant.components.binary_sensor import (
BinarySensorDeviceClass,
BinarySensorEntity,
BinarySensorEntityDescription,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from .const import CIRC_PUMP, DOMAIN, FILTER
from .const import DOMAIN
from .entity import BalboaEntity

FILTER_STATES = [
[False, False], # self.FILTER_OFF
[True, False], # self.FILTER_1
[False, True], # self.FILTER_2
[True, True], # self.FILTER_1_2
]


async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
"""Set up the spa's binary sensors."""
spa = hass.data[DOMAIN][entry.entry_id]
entities: list[BalboaSpaBinarySensor] = [
BalboaSpaFilter(entry, spa, FILTER, index) for index in range(1, 3)
spa: SpaClient = hass.data[DOMAIN][entry.entry_id]
entities = [
BalboaBinarySensorEntity(spa, description)
for description in BINARY_SENSOR_DESCRIPTIONS
]
if spa.have_circ_pump():
entities.append(BalboaSpaCircPump(entry, spa, CIRC_PUMP))

if spa.circulation_pump is not None:
entities.append(BalboaBinarySensorEntity(spa, CIRCULATION_PUMP_DESCRIPTION))
async_add_entities(entities)


class BalboaSpaBinarySensor(BalboaEntity, BinarySensorEntity):
"""Representation of a Balboa Spa binary sensor entity."""

_attr_device_class = BinarySensorDeviceClass.MOVING

@dataclass
class BalboaBinarySensorEntityDescriptionMixin:
"""Mixin for required keys."""

is_on_fn: Callable[[SpaClient], bool]
on_off_icons: tuple[str, str]


@dataclass
class BalboaBinarySensorEntityDescription(
BinarySensorEntityDescription, BalboaBinarySensorEntityDescriptionMixin
):
"""A class that describes Balboa binary sensor entities."""


FILTER_CYCLE_ICONS = ("mdi:sync", "mdi:sync-off")
BINARY_SENSOR_DESCRIPTIONS = (
BalboaBinarySensorEntityDescription(
key="filter_cycle_1",
name="Filter1",
device_class=BinarySensorDeviceClass.RUNNING,
is_on_fn=lambda spa: spa.filter_cycle_1_running,
on_off_icons=FILTER_CYCLE_ICONS,
),
BalboaBinarySensorEntityDescription(
key="filter_cycle_2",
name="Filter2",
device_class=BinarySensorDeviceClass.RUNNING,
is_on_fn=lambda spa: spa.filter_cycle_2_running,
on_off_icons=FILTER_CYCLE_ICONS,
),
)
CIRCULATION_PUMP_DESCRIPTION = BalboaBinarySensorEntityDescription(
key="circulation_pump",
name="Circ Pump",
device_class=BinarySensorDeviceClass.RUNNING,
is_on_fn=lambda spa: (pump := spa.circulation_pump) is not None and pump.state > 0,
on_off_icons=("mdi:pump", "mdi:pump-off"),
)

class BalboaSpaCircPump(BalboaSpaBinarySensor):
"""Representation of a Balboa Spa circulation pump."""

@property
def is_on(self) -> bool:
"""Return true if the filter is on."""
return self._client.get_circ_pump()

@property
def icon(self):
"""Return the icon to use in the frontend."""
return "mdi:water-pump" if self.is_on else "mdi:water-pump-off"
class BalboaBinarySensorEntity(BalboaEntity, BinarySensorEntity):
"""Representation of a Balboa Spa binary sensor entity."""

entity_description: BalboaBinarySensorEntityDescription

class BalboaSpaFilter(BalboaSpaBinarySensor):
"""Representation of a Balboa Spa Filter."""
def __init__(
self, spa: SpaClient, description: BalboaBinarySensorEntityDescription
) -> None:
"""Initialize a Balboa binary sensor entity."""
super().__init__(spa, description.name)
self.entity_description = description

@property
def is_on(self) -> bool:
"""Return true if the filter is on."""
return FILTER_STATES[self._client.get_filtermode()][self._num - 1]
"""Return true if the binary sensor is on."""
return self.entity_description.is_on_fn(self._client)

@property
def icon(self):
"""Return the icon to use in the frontend."""
return "mdi:sync" if self.is_on else "mdi:sync-off"
def icon(self) -> str | None:
"""Return the icon to use in the frontend, if any."""
icons = self.entity_description.on_off_icons
return icons[0] if self.is_on else icons[1]
Loading

0 comments on commit 11ccd16

Please sign in to comment.