Skip to content

Commit

Permalink
Initial Sensor commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ReneNulschDE committed Jun 18, 2022
1 parent fc75fba commit c804afc
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions custom_components/mysmartbike/sensor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
"""
Sensor support for bikes with MySmartBike app.
For more details about this component, please refer to the documentation at
https://github.com/ReneNulschDE/ha-mysmartbike/
"""
import logging

from homeassistant.helpers.restore_state import RestoreEntity

from . import MySmartBikeEntity

from .const import (
DOMAIN,
SENSORS
)

LOGGER = logging.getLogger(__name__)

async def async_setup_entry(hass, entry, async_add_entities):
"""Setup the sensor platform."""

data = hass.data[DOMAIN]

if not data.client.bikes:
LOGGER.info("No Bikes found.")
return

sensor_list = []
for bike in data.client.bikes:

for key, value in sorted(SENSORS.items()):
device = MySmartBikeSensor(
hass=hass,
data=data,
internal_name = key,
sensor_config = value,
vin = bike.finorvin
)
sensor_list.append(device)

async_add_entities(sensor_list, True)




class MySmartBikeSensor(MySmartBikeEntity, RestoreEntity):
"""Representation of a Sensor."""

@property
def state(self):
"""Return the state of the sensor."""

return self._state


async def async_added_to_hass(self) -> None:
"""Call when entity about to be added to Home Assistant."""
await super().async_added_to_hass()
# __init__ will set self._state to self._initial, only override
# if needed.
state = await self.async_get_last_state()
if state is not None:
self._state = state.state

0 comments on commit c804afc

Please sign in to comment.