Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for Ubiquiti mFi sensors."""
2 
3 from __future__ import annotations
4 
5 import logging
6 
7 from mficlient.client import FailedToLogin, MFiClient
8 import requests
9 import voluptuous as vol
10 
12  PLATFORM_SCHEMA as SENSOR_PLATFORM_SCHEMA,
13  SensorDeviceClass,
14  SensorEntity,
15 )
16 from homeassistant.const import (
17  CONF_HOST,
18  CONF_PASSWORD,
19  CONF_PORT,
20  CONF_SSL,
21  CONF_USERNAME,
22  CONF_VERIFY_SSL,
23  STATE_OFF,
24  STATE_ON,
25  UnitOfTemperature,
26 )
27 from homeassistant.core import HomeAssistant
29 from homeassistant.helpers.entity_platform import AddEntitiesCallback
30 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
31 
32 _LOGGER = logging.getLogger(__name__)
33 
34 DEFAULT_SSL = True
35 DEFAULT_VERIFY_SSL = True
36 
37 DIGITS = {"volts": 1, "amps": 1, "active_power": 0, "temperature": 1}
38 
39 SENSOR_MODELS = [
40  "Ubiquiti mFi-THS",
41  "Ubiquiti mFi-CS",
42  "Ubiquiti mFi-DS",
43  "Outlet",
44  "Input Analog",
45  "Input Digital",
46 ]
47 
48 PLATFORM_SCHEMA = SENSOR_PLATFORM_SCHEMA.extend(
49  {
50  vol.Required(CONF_HOST): cv.string,
51  vol.Required(CONF_USERNAME): cv.string,
52  vol.Required(CONF_PASSWORD): cv.string,
53  vol.Optional(CONF_PORT): cv.port,
54  vol.Optional(CONF_SSL, default=DEFAULT_SSL): cv.boolean,
55  vol.Optional(CONF_VERIFY_SSL, default=DEFAULT_VERIFY_SSL): cv.boolean,
56  }
57 )
58 
59 
61  hass: HomeAssistant,
62  config: ConfigType,
63  add_entities: AddEntitiesCallback,
64  discovery_info: DiscoveryInfoType | None = None,
65 ) -> None:
66  """Set up mFi sensors."""
67  host = config.get(CONF_HOST)
68  username = config.get(CONF_USERNAME)
69  password = config.get(CONF_PASSWORD)
70  use_tls = config.get(CONF_SSL)
71  verify_tls = config.get(CONF_VERIFY_SSL)
72  default_port = 6443 if use_tls else 6080
73  port = int(config.get(CONF_PORT, default_port))
74 
75  try:
76  client = MFiClient(
77  host, username, password, port=port, use_tls=use_tls, verify=verify_tls
78  )
79  except (FailedToLogin, requests.exceptions.ConnectionError) as ex:
80  _LOGGER.error("Unable to connect to mFi: %s", str(ex))
81  return
82 
84  MfiSensor(port, hass)
85  for device in client.get_devices()
86  for port in device.ports.values()
87  if port.model in SENSOR_MODELS
88  )
89 
90 
92  """Representation of a mFi sensor."""
93 
94  def __init__(self, port, hass):
95  """Initialize the sensor."""
96  self._port_port = port
97  self._hass_hass = hass
98 
99  @property
100  def name(self):
101  """Return the name of the sensor."""
102  return self._port_port.label
103 
104  @property
105  def native_value(self):
106  """Return the state of the sensor."""
107  try:
108  tag = self._port_port.tag
109  except ValueError:
110  tag = None
111  if tag is None:
112  return STATE_OFF
113  if self._port_port.model == "Input Digital":
114  return STATE_ON if self._port_port.value > 0 else STATE_OFF
115  digits = DIGITS.get(self._port_port.tag, 0)
116  return round(self._port_port.value, digits)
117 
118  @property
119  def device_class(self):
120  """Return the device class of the sensor."""
121  try:
122  tag = self._port_port.tag
123  except ValueError:
124  return None
125 
126  if tag == "temperature":
127  return SensorDeviceClass.TEMPERATURE
128 
129  return None
130 
131  @property
133  """Return the unit of measurement of this entity, if any."""
134  try:
135  tag = self._port_port.tag
136  except ValueError:
137  return None
138 
139  if tag == "temperature":
140  return UnitOfTemperature.CELSIUS
141  if tag == "active_pwr":
142  return "Watts"
143  if self._port_port.model == "Input Digital":
144  return None
145  return tag
146 
147  def update(self) -> None:
148  """Get the latest data."""
149  self._port_port.refresh()
def add_entities(account, async_add_entities, tracked)
Definition: sensor.py:40
None setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback add_entities, DiscoveryInfoType|None discovery_info=None)
Definition: sensor.py:65