2019-06-19 22:41:43 +00:00
|
|
|
"""Support for PCA 301 smart switch."""
|
2022-01-03 15:31:24 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2019-06-19 22:41:43 +00:00
|
|
|
import logging
|
|
|
|
|
2019-09-24 11:10:03 +00:00
|
|
|
import pypca
|
|
|
|
from serial import SerialException
|
2019-06-19 22:41:43 +00:00
|
|
|
|
2020-04-26 16:50:37 +00:00
|
|
|
from homeassistant.components.switch import ATTR_CURRENT_POWER_W, SwitchEntity
|
2019-09-24 11:10:03 +00:00
|
|
|
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
|
2022-01-03 15:31:24 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
2019-06-19 22:41:43 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_TOTAL_ENERGY_KWH = "total_energy_kwh"
|
2019-06-19 22:41:43 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DEFAULT_NAME = "PCA 301"
|
2019-06-19 22:41:43 +00:00
|
|
|
|
|
|
|
|
2022-01-03 15:31:24 +00:00
|
|
|
def setup_platform(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
|
|
|
add_entities: AddEntitiesCallback,
|
|
|
|
discovery_info: DiscoveryInfoType | None = None,
|
|
|
|
) -> None:
|
2019-06-19 22:41:43 +00:00
|
|
|
"""Set up the PCA switch platform."""
|
|
|
|
|
2019-09-24 11:10:03 +00:00
|
|
|
if discovery_info is None:
|
|
|
|
return
|
|
|
|
|
|
|
|
serial_device = discovery_info["device"]
|
2019-06-19 22:41:43 +00:00
|
|
|
|
|
|
|
try:
|
2019-09-24 11:10:03 +00:00
|
|
|
pca = pypca.PCA(serial_device)
|
2019-06-19 22:41:43 +00:00
|
|
|
pca.open()
|
2019-09-24 11:10:03 +00:00
|
|
|
|
|
|
|
entities = [SmartPlugSwitch(pca, device) for device in pca.get_devices()]
|
2019-06-19 22:41:43 +00:00
|
|
|
add_entities(entities, True)
|
|
|
|
|
|
|
|
except SerialException as exc:
|
|
|
|
_LOGGER.warning("Unable to open serial port: %s", exc)
|
|
|
|
return
|
|
|
|
|
|
|
|
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, pca.close)
|
|
|
|
|
|
|
|
pca.start_scan()
|
|
|
|
|
|
|
|
|
2020-04-26 16:50:37 +00:00
|
|
|
class SmartPlugSwitch(SwitchEntity):
|
2019-06-19 22:41:43 +00:00
|
|
|
"""Representation of a PCA Smart Plug switch."""
|
|
|
|
|
2019-09-24 11:10:03 +00:00
|
|
|
def __init__(self, pca, device_id):
|
2019-06-19 22:41:43 +00:00
|
|
|
"""Initialize the switch."""
|
|
|
|
self._device_id = device_id
|
2019-09-24 11:10:03 +00:00
|
|
|
self._name = "PCA 301"
|
2019-06-19 22:41:43 +00:00
|
|
|
self._state = None
|
|
|
|
self._available = True
|
|
|
|
self._emeter_params = {}
|
|
|
|
self._pca = pca
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the Smart Plug, if any."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def available(self) -> bool:
|
|
|
|
"""Return if switch is available."""
|
|
|
|
return self._available
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return true if switch is on."""
|
|
|
|
return self._state
|
|
|
|
|
|
|
|
def turn_on(self, **kwargs):
|
|
|
|
"""Turn the switch on."""
|
|
|
|
self._pca.turn_on(self._device_id)
|
|
|
|
|
|
|
|
def turn_off(self, **kwargs):
|
|
|
|
"""Turn the switch off."""
|
|
|
|
self._pca.turn_off(self._device_id)
|
|
|
|
|
|
|
|
@property
|
2021-03-11 15:51:03 +00:00
|
|
|
def extra_state_attributes(self):
|
2019-06-19 22:41:43 +00:00
|
|
|
"""Return the state attributes of the device."""
|
|
|
|
return self._emeter_params
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Update the PCA switch's state."""
|
|
|
|
try:
|
2020-02-24 16:47:52 +00:00
|
|
|
self._emeter_params[
|
|
|
|
ATTR_CURRENT_POWER_W
|
|
|
|
] = f"{self._pca.get_current_power(self._device_id):.1f}"
|
|
|
|
self._emeter_params[
|
|
|
|
ATTR_TOTAL_ENERGY_KWH
|
|
|
|
] = f"{self._pca.get_total_consumption(self._device_id):.2f}"
|
2019-06-19 22:41:43 +00:00
|
|
|
|
|
|
|
self._available = True
|
|
|
|
self._state = self._pca.get_state(self._device_id)
|
|
|
|
|
|
|
|
except (OSError) as ex:
|
|
|
|
if self._available:
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.warning("Could not read state for %s: %s", self.name, ex)
|
2019-06-19 22:41:43 +00:00
|
|
|
self._available = False
|