-
Notifications
You must be signed in to change notification settings - Fork 1
/
sensor.py
133 lines (106 loc) · 3.91 KB
/
sensor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
"""Platform for sensor integration."""
from __future__ import annotations
import logging
from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity,
SensorStateClass
)
from homeassistant.const import CURRENCY_CENT
from homeassistant.core import HomeAssistant
from homeassistant.helpers.typing import DiscoveryInfoType
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.config_entries import ConfigEntry
from fuelwatcher import FuelWatch
from .const import DOMAIN
_LOGGER = logging.getLogger(DOMAIN)
async def async_setup_entry(
hass: HomeAssistant,
config: ConfigEntry,
async_add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None
) -> None:
"""Set up the sensor platform."""
async_add_entities([
FuelPrice(hass, config),
FuelStationName(hass, config),
FuelStationLocation(hass, config),
FuelStationAddress(hass, config)
])
class FuelWatchSensor(SensorEntity):
"""Base class for FuelWatch sensors."""
api = FuelWatch()
def __init__(
self,
hass: HomeAssistant,
config_entries: ConfigEntry,
attr_name: str,
xml_key: str
):
self._hass = hass
self._attr_name = attr_name
self._attr_unique_id = f"sensor.fuelwatchwa_{xml_key.lower()}"
self.xml_query = None
self._attr_native_value = None
self._fuel_type = config_entries.data['product']
self._suburb = config_entries.data['suburb']
self._day = config_entries.data['day']
self._xml_key = xml_key
@property
def fuel_type(self) -> int:
"""Return the Fuel Type specified"""
return self._fuel_type
@property
def suburb(self) -> str:
"""Return the Suburb for the fuel search"""
return self._suburb
@property
def day(self) -> str:
"""Return the day of the fuel price"""
return self._day
def update(self) -> None:
"""Fetch new state data for the sensor.
This is the only method that should fetch new data for Home Assistant.
"""
self.api.query(product=self._fuel_type, suburb=self._suburb, day=self._day)
self.xml_query = self.api.get_xml
self._attr_native_value = self.xml_query[0][self._xml_key]
class FuelPrice(FuelWatchSensor):
"""Representation of a Sensor for the cheapest fuel price today."""
_attr_native_unit_of_measurement = CURRENCY_CENT
_attr_device_class = SensorDeviceClass.MONETARY
_attr_state_class = SensorStateClass.MEASUREMENT
def __init__(self, hass: HomeAssistant, config_entries: ConfigEntry) -> None:
super().__init__(
hass=hass,
config_entries=config_entries,
attr_name="Fuel Cheapest Price",
xml_key="price"
)
class FuelStationName(FuelWatchSensor):
"""Representation of a Sensor for the station name."""
def __init__(self, hass: HomeAssistant, config_entries: ConfigEntry) -> None:
super().__init__(
hass=hass,
config_entries=config_entries,
attr_name="Fuel Station Name",
xml_key="brand"
)
class FuelStationLocation(FuelWatchSensor):
"""Representation of a Sensor for the station location."""
def __init__(self, hass: HomeAssistant, config_entries: ConfigEntry) -> None:
super().__init__(
hass=hass,
config_entries=config_entries,
attr_name="Fuel Station Suburb",
xml_key="location"
)
class FuelStationAddress(FuelWatchSensor):
"""Representation of a Sensor for the station address."""
def __init__(self, hass: HomeAssistant, config_entries: ConfigEntry) -> None:
super().__init__(
hass=hass,
config_entries=config_entries,
attr_name="Fuel Station Address",
xml_key="address"
)