Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Translations and refactor #8

Merged
merged 2 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions custom_components/smartbox/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

import logging
from typing import Any
import requests
from smartbox import Session

import requests
from homeassistant import exceptions
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryAuthFailed
from smartbox import Session

from .const import (
CONF_API_NAME,
Expand All @@ -32,8 +33,6 @@
Platform.SWITCH,
]

from homeassistant import exceptions


class InvalidAuth(exceptions.HomeAssistantError):
"""Error to indicate there is invalid auth."""
Expand Down
33 changes: 5 additions & 28 deletions custom_components/smartbox/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_LOCKED, ATTR_TEMPERATURE, UnitOfTemperature
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from .const import (
Expand All @@ -29,6 +28,7 @@
PRESET_SELF_LEARN,
SMARTBOX_NODES,
)
from .entity import SmartBoxNodeEntity
from .model import (
SmartboxNode,
_check_status_key,
Expand Down Expand Up @@ -62,16 +62,16 @@ async def async_setup_entry(
_LOGGER.debug("Finished setting up Smartbox climate platform")


class SmartboxHeater(ClimateEntity):
class SmartboxHeater(SmartBoxNodeEntity, ClimateEntity):
"""Smartbox heater climate control."""

_attr_translation_key = "thermostat"
_attr_key = "thermostat"
_attr_name = None

def __init__(self, node: MagicMock | SmartboxNode) -> None:
"""Initialize the sensor."""
_LOGGER.debug("Setting up Smartbox climate platerqgsdform")
self._node = node
super().__init__(node=node)
self._status: dict[str, Any] = {}
self._available = False # unavailable until we get an update
self._enable_turn_on_off_backwards_compatibility = False
Expand All @@ -81,9 +81,7 @@ def __init__(self, node: MagicMock | SmartboxNode) -> None:
| ClimateEntityFeature.TURN_OFF
| ClimateEntityFeature.TURN_ON
)
self._device_id = self._node.node_id
self._attr_unique_id = self._node.node_id
_LOGGER.debug("Created node %s unique_id=%s", self.name, self.unique_id)
_LOGGER.debug("Created node unique_id=%s", self.unique_id)

async def async_turn_off(self) -> None:
"""Turn off hvac."""
Expand All @@ -93,27 +91,6 @@ async def async_turn_on(self) -> None:
"""Turn on hvac."""
await self.async_set_hvac_mode(HVACMode.AUTO)

@property
def device_info(self) -> DeviceInfo:
"""Return the device info."""
return DeviceInfo(
identifiers={(DOMAIN, self._device_id)},
name=self._node.name,
model_id=self._node.device.model_id,
sw_version=self._node.device.sw_version,
serial_number=self._node.device.serial_number,
)

@property
def unique_id(self) -> str:
"""Return Unique ID string."""
return f"{self._node.node_id}_climate"

@property
def name(self) -> str:
"""Return the name of the sensor."""
return f"{self._node.name}"

@property
def supported_features(self) -> int:
"""Return the list of supported features."""
Expand Down
7 changes: 2 additions & 5 deletions custom_components/smartbox/config_flow.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
"""Config flow for Smartbox."""

from collections.abc import Mapping
import copy
import logging
from typing import Any

import homeassistant.helpers.config_validation as cv
import requests
import voluptuous as vol

from homeassistant.config_entries import (
ConfigEntry,
ConfigFlow,
Expand All @@ -16,14 +14,13 @@
OptionsFlow,
)
from homeassistant.core import callback
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.selector import (
TextSelector,
TextSelectorConfig,
TextSelectorType,
)

from . import create_smartbox_session_from_entry, InvalidAuth
from . import InvalidAuth, create_smartbox_session_from_entry
from .const import (
CONF_API_NAME,
CONF_BASIC_AUTH_CREDS,
Expand Down
82 changes: 50 additions & 32 deletions custom_components/smartbox/entity.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,52 @@
"""Draft of generic entity"""

# from unittest.mock import MagicMock

# from custom_components.smartbox.const import DOMAIN
# from custom_components.smartbox.model import SmartboxNode
# from homeassistant.helpers.entity import DeviceInfo, Entity


# class SmartBoxEntity(Entity):
# """BaseClass for entities."""

# def __init__(self, node: SmartboxNode | MagicMock) -> None:
# """Initialize the away Entity."""
# self._node = node
# self._device = self._node.device
# self._device_id = self._node.node_id
# self._attr_has_entity_name = True

# @property
# def unique_id(self) -> str:
# """Return the unique id of the switch."""
# return f"{self._device_id}_{self._attr_translation_key}"

# @property
# def device_info(self) -> DeviceInfo:
# """Return the device info."""
# return DeviceInfo(
# identifiers={(DOMAIN, self._device_id)},
# name=self._device.name,
# model_id=self._device.model_id,
# sw_version=self._device.sw_version,
# serial_number=self._device.serial_number,
# )
from homeassistant.helpers.entity import DeviceInfo, Entity

from custom_components.smartbox.const import DOMAIN
from custom_components.smartbox.model import SmartboxDevice, SmartboxNode


class DefaultSmartBoxEntity(Entity):
"""Default Smartbox Entity."""

def __init__(self) -> None:
"""Initialize the default Device Entity."""
self._device_id = self._node.node_id
self._attr_has_entity_name = True
self._attr_translation_key = self._attr_key
self._attr_unique_id = self._node.node_id

@property
def unique_id(self) -> str:
"""Return Unique ID string."""
return f"{self._device_id}_{self._attr_key}"

@property
def device_info(self) -> DeviceInfo:
"""Return the device info."""
return DeviceInfo(
identifiers={(DOMAIN, self._device_id)},
name=self._node.name,
model_id=self._node.device.model_id,
sw_version=self._node.device.sw_version,
serial_number=self._node.device.serial_number,
)


class SmartBoxDeviceEntity(DefaultSmartBoxEntity):
"""BaseClass for SmartBoxDeviceEntity."""

def __init__(self, device: SmartboxDevice) -> None:
"""Initialize the Device Entity."""
self._node = list(device.get_nodes())[0]
self._device = device
super().__init__()


class SmartBoxNodeEntity(DefaultSmartBoxEntity):
"""BaseClass for SmartBoxNodeEntity."""

def __init__(self, node: SmartboxNode) -> None:
"""Initialize the Node Entity."""
self._node = node
super().__init__()
5 changes: 2 additions & 3 deletions custom_components/smartbox/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
from typing import Any, cast
from unittest.mock import MagicMock

from smartbox import Session, UpdateManager

from homeassistant.components.climate import (
PRESET_ACTIVITY,
PRESET_AWAY,
Expand All @@ -17,6 +15,7 @@
)
from homeassistant.const import UnitOfTemperature
from homeassistant.core import HomeAssistant
from smartbox import Session, UpdateManager

from .const import (
GITHUB_ISSUES_URL,
Expand Down Expand Up @@ -185,7 +184,7 @@ def __init__(
@property
def node_id(self) -> str:
"""Return the id of the node."""
return f"{self._device.dev_id}-{self._node_info['addr']}"
return f"{self._device.dev_id}_{self._node_info['addr']}"

@property
def name(self) -> str:
Expand Down
36 changes: 4 additions & 32 deletions custom_components/smartbox/number.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
"""Support for Smartbox sensor entities."""

import logging
from unittest.mock import MagicMock

from homeassistant.components.number import NumberEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from .const import DOMAIN, SMARTBOX_DEVICES
from .model import SmartboxDevice
from .entity import SmartBoxDeviceEntity

_LOGGER = logging.getLogger(__name__)
_MAX_POWER_LIMIT = 9999
Expand All @@ -23,44 +21,18 @@ async def async_setup_entry(
_LOGGER.debug("Setting up Smartbox number platform")

async_add_entities(
[DevicePowerLimit(device) for device in hass.data[DOMAIN][SMARTBOX_DEVICES]],
[PowerLimit(device) for device in hass.data[DOMAIN][SMARTBOX_DEVICES]],
True,
)

_LOGGER.debug("Finished setting up Smartbox number platform")


class DevicePowerLimit(NumberEntity):
class PowerLimit(SmartBoxDeviceEntity, NumberEntity):
"""Smartbox device power limit."""

def __init__(self, device: SmartboxDevice | MagicMock) -> None:
"""Initialize the Entity."""
self._device = device
self._device_id = list(device.get_nodes())[0].node_id

_attr_key = "power_limit"
native_max_value: float = _MAX_POWER_LIMIT

@property
def device_info(self) -> DeviceInfo:
"""Return the device info."""
return DeviceInfo(
identifiers={(DOMAIN, self._device_id)},
name=self._device.name,
model_id=self._device.model_id,
sw_version=self._device.sw_version,
serial_number=self._device.serial_number,
)

@property
def name(self):
"""Return the name of the number."""
return f"{self._device.name} Power Limit"

@property
def unique_id(self) -> str:
"""Return the unique id of the number."""
return f"{self._device.dev_id}_power_limit"

@property
def native_value(self) -> float:
"""Return the native value of the number."""
Expand Down
Loading
Loading