1 """Support for the Tank Utility propane monitor."""
3 from __future__
import annotations
9 from tank_utility
import auth, device
as tank_monitor
10 import voluptuous
as vol
13 PLATFORM_SCHEMA
as SENSOR_PLATFORM_SCHEMA,
22 _LOGGER = logging.getLogger(__name__)
24 SCAN_INTERVAL = datetime.timedelta(hours=1)
26 PLATFORM_SCHEMA = SENSOR_PLATFORM_SCHEMA.extend(
28 vol.Required(CONF_EMAIL): cv.string,
29 vol.Required(CONF_PASSWORD): cv.string,
30 vol.Required(CONF_DEVICES): vol.All(cv.ensure_list, vol.Length(min=1)),
35 SENSOR_ROUNDING_PRECISION = 1
51 add_entities: AddEntitiesCallback,
52 discovery_info: DiscoveryInfoType |
None =
None,
54 """Set up the Tank Utility sensor."""
56 email = config[CONF_EMAIL]
57 password = config[CONF_PASSWORD]
58 devices = config[CONF_DEVICES]
61 token = auth.get_token(email, password)
62 except requests.exceptions.HTTPError
as http_error:
63 if http_error.response.status_code == requests.codes.unauthorized:
64 _LOGGER.error(
"Invalid credentials")
68 for device
in devices:
70 all_sensors.append(sensor)
75 """Representation of a Tank Utility sensor."""
77 def __init__(self, email, password, token, device):
78 """Initialize the sensor."""
84 self.
_name_name = f
"Tank Utility {self.device}"
90 """Return the device identifier."""
95 """Return the state of the device."""
100 """Return the name of the device."""
101 return self.
_name_name
105 """Return the unit of measurement of the device."""
110 """Return the attributes of the device."""
114 """Get data from the device.
116 Flatten dictionary to map device to map of device data.
122 data = tank_monitor.get_device_data(self.
_token_token, self.
devicedevice)
123 except requests.exceptions.HTTPError
as http_error:
124 if http_error.response.status_code
in (
125 requests.codes.unauthorized,
126 requests.codes.bad_request,
128 _LOGGER.debug(
"Getting new token")
130 data = tank_monitor.get_device_data(self.
_token_token, self.
devicedevice)
133 data.update(data.pop(
"device", {}))
134 data.update(data.pop(
"lastReading", {}))
138 """Set the device state and attributes."""
140 self.
_state_state = round(data[SENSOR_TYPE], SENSOR_ROUNDING_PRECISION)
141 self.
_attributes_attributes = {k: v
for k, v
in data.items()
if k
in SENSOR_ATTRS}
def native_unit_of_measurement(self)
def extra_state_attributes(self)
def __init__(self, email, password, token, device)
def add_entities(account, async_add_entities, tracked)
None setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback add_entities, DiscoveryInfoType|None discovery_info=None)