Logs will tell
? In the System Log is no entry from the sensor.py _LOGGER Code…
What did you mean with ‘Logs will tell’?
The Logs says nothing - but I think I import the serial-Library the wrong way…
Could anybody make a litte example, how I could send and receive something from my own integration?
Your log will tell you if HA is even seeing your code.
grep serial /config/home-assistant.log
Can you make sure you enabled logging properly?
i.e.:
logger:
default: error
logs:
homeassistant.components.serial: debug
The logger is in my .py (it looks like you would define it in the config.yaml)
Here is my example hub.py (downloaded example from detailed_hello_world_push with added the “import serial” and the ser.write commands)
I mean this:
ser = serial.Serial ("/dev/serial0", 4800) #Open port with baud rate
ser.write(str.encode('Hallo aus HA'))
in here:
"""A demonstration 'hub' that connects several devices."""
from __future__ import annotations
# In a real implementation, this would be in an external library that's on PyPI.
# The PyPI package needs to be included in the `requirements` section of manifest.json
# See https://developers.home-assistant.io/docs/creating_integration_manifest
# for more information.
# This dummy hub always returns 3 rollers.
import asyncio
import random
import logging
import serial
from homeassistant.core import HomeAssistant
_LOGGER = logging.getLogger(__name__)
class Hub:
"""Dummy hub for Hello World example."""
manufacturer = "test_manufacturer"
def __init__(self, hass: HomeAssistant, host: str, ase_model: str) -> None:
"""Init dummy hub."""
self._host = host
self._hass = hass
self._name = host
self._model = ase_model
self._id = host.lower()
self.rollers = [
Roller(f"{self._id}_1", f"{self._name} 1", self._model, self),
#Roller(f"{self._id}_2", f"{self._name} 2", self),
#Roller(f"{self._id}_3", f"{self._name} 3", self),
]
self.online = True
@property
def hub_id(self) -> str:
"""ID for dummy hub."""
return self._id
async def test_connection(self) -> bool:
"""Test connectivity to the Dummy hub is OK."""
await asyncio.sleep(1)
return True
class Roller:
"""Dummy roller (device for HA) for Hello World example."""
def __init__(self, rollerid: str, name: str, model: str, hub: Hub) -> None:
"""Init dummy roller."""
self._id = rollerid
self.hub = hub
self.name = name
self._callbacks = set()
self._loop = asyncio.get_event_loop()
self._target_position = 100
self._current_position = 100
# Reports if the roller is moving up or down.
# >0 is up, <0 is down. This very much just for demonstration.
self.moving = 0
# Some static information about this device
self.firmware_version = f"0.0.{random.randint(1, 9)}"
self.model = model
@property
def roller_id(self) -> str:
"""Return ID for roller."""
return self._id
@property
def position(self):
"""Return position for roller."""
return self._current_position
async def set_position(self, position: int) -> None:
"""
Set dummy cover to the given position.
State is announced a random number of seconds later.
"""
_LOGGER.exception("Roller set_position")
_LOGGER.debug("Roller set_position")
ser = serial.Serial ("/dev/serial0", 4800) #Open port with baud rate
ser.write(str.encode('Hallo aus HA'))
number_as_string = str(position)
ser.write(str.encode(number_as_string))
self._target_position = position
# Update the moving status, and broadcast the update
self.moving = position - 50
await self.publish_updates()
self._loop.create_task(self.delayed_update())
async def delayed_update(self) -> None:
"""Publish updates, with a random delay to emulate interaction with device."""
await asyncio.sleep(random.randint(1, 10))
self.moving = 0
_LOGGER.exception("Roller delayed_update")
await self.publish_updates()
def register_callback(self, callback: Callable[[], None]) -> None:
"""Register callback, called when Roller changes state."""
self._callbacks.add(callback)
def remove_callback(self, callback: Callable[[], None]) -> None:
"""Remove previously registered callback."""
self._callbacks.discard(callback)
# In a real implementation, this library would call it's call backs when it was
# notified of any state changeds for the relevant device.
async def publish_updates(self) -> None:
"""Schedule call all registered callbacks."""
self._current_position = self._target_position
for callback in self._callbacks:
callback()
@property
def online(self) -> float:
"""Roller is online."""
# The dummy roller is offline about 10% of the time. Returns True if online,
# False if offline.
return random.random() > 0.1
@property
def battery_level(self) -> int:
"""Battery level as a percentage."""
return random.randint(0, 100)
@property
def battery_voltage(self) -> float:
"""Return a random voltage roughly that of a 12v battery."""
return round(random.random() * 3 + 10, 2)
@property
def illuminance(self) -> int:
"""Return a sample illuminance in lux."""
return random.randint(0, 500)
But I think this is not the recommended way to send or receive serial data…
Could someone tell me, how I could import and use the serial library from HA?
(I think this is this one: serial-library, am I right?)