Skip to content

Commit

Permalink
Improve logging and add more sensors
Browse files Browse the repository at this point in the history
  • Loading branch information
FaserF committed Aug 29, 2023
1 parent f8402a0 commit 66221b2
Showing 1 changed file with 188 additions and 24 deletions.
212 changes: 188 additions & 24 deletions custom_components/chefkoch/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,24 @@ async def async_setup_entry(
_LOGGER.debug("Sensor async_setup_entry")
if entry.options:
config.update(entry.options)
sensors = ChefkochSensor(config, hass)
async_add_entities(sensors, update_before_add=True)
async_add_entities(
[
ChefkochSensor(config, hass)
ChefkochSensorDaily(config, hass),
ChefkochSensorRandom(config, hass),
ChefkochSensorDailyBacke(config, hass),
],
update_before_add=True
)

class ChefkochSensor(SensorEntity):
"""Implementation of a Deutsche Bahn sensor."""

class ChefkochSensorDaily(SensorEntity):
def __init__(self, config, hass: HomeAssistantType):
super().__init__()
self._name = "Chefkoch daily recommendations"
self._state = None
self._available = True
self.hass = hass
self.updated = datetime.now()
self.chefkoch = chefkoch.chefkoch()
self.attrs: None

@property
def name(self):
Expand Down Expand Up @@ -91,33 +89,199 @@ async def async_update(self):
hass = self.hass
"""Pull data from the chefkoch.de web page."""
data = await hass.async_add_executor_job(
fetch_chefkoch_data, hass, self
fetch_chefkoch_daily_recipies, hass, self
)
recipes_count = len(data)

if recipes_count > 0:

recipes = []
for recipe in data:
_LOGGER.debug(f"Processing recipe: '{recipe}")
recipes.append(
{
ATTR_RECIPE: recipe,
}
)

# Get the amount of offers
recipes_count = len(recipes)

self.attrs[ATTR_RECIPES] = recipes
self.attrs[ATTR_ATTRIBUTION] = f"last updated {datetime.now()} \n{ATTRIBUTION}"
self._state = recipes_count
self._available = True
else:
_LOGGER.exception(f"Data from chefkoch for daily was empty, retrying at next sync run. Maybe also check your internet connection?")
self._available = False

except:
self._available = False
_LOGGER.exception(f"Cannot retrieve data for '{self._name}'")

class ChefkochSensorRandom(SensorEntity):
def __init__(self, config, hass: HomeAssistantType):
super().__init__()
self._name = "Chefkoch random recommendations"
self._state = None
self._available = True
self.hass = hass
self.updated = datetime.now()
self.attrs: None

@property
def name(self):
"""Return the name of the sensor."""
return self._name

recipes = []
for recipe in data['categories']:
_LOGGER.debug(f"Processing recipe: '{recipe}")
recipes.append(
{
ATTR_RECIPE: item['title'],
}
@property
def unique_id(self) -> str:
"""Return the unique ID of the sensor."""
return self._name

@property
def icon(self):
"""Return the icon for the frontend."""
return "mdi:chef-hat"

@property
def state(self) -> Optional[str]:
if self._state is not None:
return self._state
else:
return "Unknown"

@property
def native_value(self):
"""Return the chefkoch data."""
return self._state

async def async_update(self):
try:
with async_timeout.timeout(30):
hass = self.hass
"""Pull data from the chefkoch.de web page."""
data = await hass.async_add_executor_job(
fetch_chefkoch_random_recipies, hass, self
)
recipes_count = len(data)

# Get the amount of offers
recipes_count = len(recipes)
if recipes_count > 0:

self.attrs[ATTR_RECIPES] = recipes
self.attrs[ATTR_ATTRIBUTION] = f"last updated {datetime.now()} \n{ATTRIBUTION}"
self._state = recipes_count
self._available = True
recipes = []
for recipe in data:
_LOGGER.debug(f"Processing recipe: '{recipe}")
recipes.append(
{
ATTR_RECIPE: recipe,
}
)

# Get the amount of offers
recipes_count = len(recipes)

self.attrs[ATTR_RECIPES] = recipes
self.attrs[ATTR_ATTRIBUTION] = f"last updated {datetime.now()} \n{ATTRIBUTION}"
self._state = recipes_count
self._available = True
else:
_LOGGER.exception(f"Data from chefkoch for random was empty, retrying at next sync run. Maybe also check your internet connection?")
self._available = False

except:
self._available = False
_LOGGER.exception(f"Cannot retrieve data for '{self._name}'")

def fetch_chefkoch_data(hass, self):
class ChefkochSensorDailyBacke(SensorEntity):
def __init__(self, config, hass: HomeAssistantType):
super().__init__()
self._name = "Chefkoch daily backe recommendations"
self._state = None
self._available = True
self.hass = hass
self.updated = datetime.now()
self.attrs: None

@property
def name(self):
"""Return the name of the sensor."""
return self._name

@property
def unique_id(self) -> str:
"""Return the unique ID of the sensor."""
return self._name

@property
def icon(self):
"""Return the icon for the frontend."""
return "mdi:chef-hat"

@property
def state(self) -> Optional[str]:
if self._state is not None:
return self._state
else:
return "Unknown"

@property
def native_value(self):
"""Return the chefkoch data."""
return self._state

async def async_update(self):
try:
with async_timeout.timeout(30):
hass = self.hass
"""Pull data from the chefkoch.de web page."""
data = await hass.async_add_executor_job(
fetch_chefkoch_random_recipies, hass, self
)
recipes_count = len(data)

if recipes_count > 0:

recipes = []
for recipe in data:
_LOGGER.debug(f"Processing recipe: '{recipe}")
recipes.append(
{
ATTR_RECIPE: recipe,
}
)

# Get the amount of offers
recipes_count = len(recipes)

self.attrs[ATTR_RECIPES] = recipes
self.attrs[ATTR_ATTRIBUTION] = f"last updated {datetime.now()} \n{ATTRIBUTION}"
self._state = recipes_count
self._available = True
else:
_LOGGER.exception(f"Data from chefkoch for daily backe was empty, retrying at next sync run. Maybe also check your internet connection?")
self._available = False

except:
self._available = False
_LOGGER.exception(f"Cannot retrieve data for '{self._name}'")

def fetch_chefkoch_daily_recipies(hass, self):
_LOGGER.debug(f"Fetching update from chefkoch python module for '{self._name}'")
data = chefkoch.get_daily_recommendations(category="koche")

_LOGGER.debug(f"Fetched daily data: {data}")
return data

def fetch_chefkoch_daily_recipies_backe(hass, self):
_LOGGER.debug(f"Fetching update from chefkoch python module for '{self._name}'")
data = chefkoch.get_daily_recommendations(category="backe")

_LOGGER.debug(f"Fetched daily backe data: {data}")
return data

def fetch_chefkoch_random_recipies(hass, self):
_LOGGER.debug(f"Fetching update from chefkoch python module for '{self._name}'")
data = chefkoch.get_daily_recommendations
data = chefkoch.get_random_recipe()

_LOGGER.debug(f"Fetched data: {data}")
_LOGGER.debug(f"Fetched random data: {data}")
return data

0 comments on commit 66221b2

Please sign in to comment.