@@ -440,9 +440,13 @@ class EverythingPresenceOneDevice extends Homey.Device {
440440 }
441441
442442 // Subscribe to entity events
443- entity . on ( `state` , ( state : unknown ) =>
444- this . onEntityState ( parseEntityResult . data . config . objectId , state )
445- ) ;
443+ entity . on ( `state` , ( state : unknown ) => {
444+ try {
445+ this . onEntityState ( parseEntityResult . data . config . objectId , state ) ;
446+ } catch ( err ) {
447+ this . debugEntity ( 'Failed to handle entity state event' , err ) ;
448+ }
449+ } ) ;
446450 }
447451
448452 /**
@@ -452,17 +456,8 @@ class EverythingPresenceOneDevice extends Homey.Device {
452456 * @param state
453457 */
454458 onEntityState ( entityId : string , state : unknown ) {
455- const parseResult = entityStateSchema . safeParse ( state ) ;
456- if ( ! parseResult . success ) {
457- this . debugEntity (
458- `Got invalid entity state for entityId ${ entityId } , error:` ,
459- parseResult . error ,
460- state
461- ) ;
462- return ;
463- }
464-
465- const parsedState = parseResult . data ;
459+ // Skip parsing which may cause CPU spikes
460+ const parsedState = state as z . infer < typeof entityStateSchema > ;
466461
467462 // Get entity
468463 const entity = this . entities . get ( entityId ) ?. data ;
@@ -479,50 +474,50 @@ class EverythingPresenceOneDevice extends Homey.Device {
479474
480475 switch ( entity . config . deviceClass ) {
481476 case 'temperature' :
482- // Throw when state is not a number
483- z . number ( ) . parse ( parsedState . state ) ;
484- this . debugEntity ( `Capability: measure_temperature: state event` , parsedState . state ) ;
485- this . setCapabilityValue ( ' measure_temperature', parsedState . state ) . catch ( ( err ) =>
486- this . debugEntity ( 'Failed to set measure_temperature capability value' , err )
487- ) ;
477+ if ( typeof parsedState ?. state === ' number' ) {
478+ this . debugEntity ( `Capability: measure_temperature: state event` , parsedState ? .state ) ;
479+ this . setCapabilityValue ( ' measure_temperature' , parsedState ? .state ) . catch ( ( err ) =>
480+ this . debugEntity ( 'Failed to set measure_temperature capability value ', err )
481+ ) ;
482+ }
488483 break ;
489484 case 'humidity' :
490- // Throw when state is not a number
491- z . number ( ) . parse ( parsedState . state ) ;
492- this . debugEntity ( `Capability: measure_humidity: state event` , parsedState . state ) ;
493- this . setCapabilityValue ( ' measure_humidity', parsedState . state ) . catch ( ( err ) =>
494- this . debugEntity ( 'Failed to set measure_humidity capability value' , err )
495- ) ;
485+ if ( typeof parsedState ?. state === ' number' ) {
486+ this . debugEntity ( `Capability: measure_humidity: state event` , parsedState ? .state ) ;
487+ this . setCapabilityValue ( ' measure_humidity' , parsedState ? .state ) . catch ( ( err ) =>
488+ this . debugEntity ( 'Failed to set measure_humidity capability value ', err )
489+ ) ;
490+ }
496491 break ;
497492 case 'illuminance' :
498- // Throw when state is not a number
499- z . number ( ) . parse ( parsedState . state ) ;
500- this . debugEntity ( `Capability: measure_luminance: state event` , parsedState . state ) ;
501- this . setCapabilityValue ( ' measure_luminance', parsedState . state ) . catch ( ( err ) =>
502- this . debugEntity ( 'Failed to set measure_luminance capability value' , err )
503- ) ;
493+ if ( typeof parsedState ?. state === ' number' ) {
494+ this . debugEntity ( `Capability: measure_luminance: state event` , parsedState ? .state ) ;
495+ this . setCapabilityValue ( ' measure_luminance' , parsedState ? .state ) . catch ( ( err ) =>
496+ this . debugEntity ( 'Failed to set measure_luminance capability value ', err )
497+ ) ;
498+ }
504499 break ;
505500 case 'motion' :
506- // Throw when state is not a boolean
507- z . boolean ( ) . parse ( parsedState . state ) ;
508- this . debugEntity ( `Capability: alarm_motion.pir: state event` , parsedState . state ) ;
509- this . setCapabilityValue ( ' alarm_motion.pir', parsedState . state ) . catch ( ( err ) =>
510- this . debugEntity ( 'Failed to set alarm_motion.pir capability value' , err )
511- ) ;
501+ if ( typeof parsedState ?. state === ' boolean' ) {
502+ this . debugEntity ( `Capability: alarm_motion.pir: state event` , parsedState ? .state ) ;
503+ this . setCapabilityValue ( ' alarm_motion.pir' , parsedState ? .state ) . catch ( ( err ) =>
504+ this . debugEntity ( 'Failed to set alarm_motion.pir capability value ', err )
505+ ) ;
506+ }
512507 break ;
513508 case 'occupancy' :
514- // Throw when state is not a boolean
515- z . boolean ( ) . parse ( parsedState . state ) ;
516- if ( includesBinarySensorMMWave ( entity ) ) {
517- this . debugEntity ( `Capability: alarm_motion.mmwave: state event` , parsedState . state ) ;
518- this . setCapabilityValue ( ' alarm_motion.mmwave', parsedState . state ) . catch ( ( err ) =>
519- this . debugEntity ( 'Failed to set alarm_motion.mmwave capability value' , err )
520- ) ;
521- } else if ( includesBinarySensorOccupancy ( entity ) ) {
522- this . debugEntity ( `Capability: alarm_motion: state event` , parsedState . state ) ;
523- this . setCapabilityValue ( ' alarm_motion', parsedState . state ) . catch ( ( err ) =>
524- this . debugEntity ( 'Failed to set alarm_motion capability value' , err )
525- ) ;
509+ if ( typeof parsedState ?. state === ' boolean' ) {
510+ if ( includesBinarySensorMMWave ( entity ) ) {
511+ this . debugEntity ( `Capability: alarm_motion.mmwave: state event` , parsedState ?. state ) ;
512+ this . setCapabilityValue ( ' alarm_motion.mmwave' , parsedState ? .state ) . catch ( ( err ) =>
513+ this . debugEntity ( 'Failed to set alarm_motion.mmwave capability value ', err )
514+ ) ;
515+ } else if ( includesBinarySensorOccupancy ( entity ) ) {
516+ this . debugEntity ( `Capability: alarm_motion: state event` , parsedState ?. state ) ;
517+ this . setCapabilityValue ( ' alarm_motion' , parsedState ? .state ) . catch ( ( err ) =>
518+ this . debugEntity ( 'Failed to set alarm_motion capability value ', err )
519+ ) ;
520+ }
526521 }
527522 break ;
528523 default :
@@ -535,32 +530,31 @@ class EverythingPresenceOneDevice extends Homey.Device {
535530 case DRIVER_SETTINGS . MMWAVE_ON_LATENCY :
536531 case DRIVER_SETTINGS . MMWAVE_OFF_LATENCY :
537532 case DRIVER_SETTINGS . MMWAVE_DISTANCE :
538- // Throw when state is not a number
539- z . number ( ) . parse ( parsedState . state ) ;
540- this . debugEntity ( `Setting: ${ entity . config . objectId } : state event` , parsedState . state ) ;
541- this . setSettings ( {
542- [ entity . config . objectId ] : parsedState . state
543- } ) . catch ( ( err ) =>
544- this . debugEntity (
545- `Failed to set setting ${ entity . config . objectId } to value: ${ parsedState . state } , reason:` ,
546- err
547- )
548- ) ;
533+ if ( typeof parsedState ?. state === ' number' ) {
534+ this . debugEntity ( `Setting: ${ entity . config . objectId } : state event` , parsedState ? .state ) ;
535+ this . setSettings ( {
536+ [ entity . config . objectId ] : parsedState ?. state
537+ } ) . catch ( ( err ) =>
538+ this . debugEntity (
539+ `Failed to set setting ${ entity . config . objectId } to value: ${ parsedState ?. state } , reason:` ,
540+ err
541+ )
542+ ) ;
543+ }
549544 break ;
550545 case DRIVER_SETTINGS . MMWAVE_LED :
551546 case DRIVER_SETTINGS . ESP_32_STATUS_LED :
552- // Throw when state is not a boolean
553- z . boolean ( ) . parse ( parsedState . state ) ;
554- this . debugEntity ( `Setting: ${ entity . config . objectId } : state event` , parsedState . state ) ;
555- this . setSettings ( {
556- [ entity . config . objectId ] : parsedState . state
557- } ) . catch ( ( err ) =>
558- this . debugEntity (
559- `Failed to set setting ${ entity . config . objectId } to value: ${ parsedState . state } , reason:` ,
560- err
561- )
562- ) ;
563-
547+ if ( typeof parsedState ?. state === 'boolean' ) {
548+ this . debugEntity ( `Setting: ${ entity . config . objectId } : state event` , parsedState ?. state ) ;
549+ this . setSettings ( {
550+ [ entity . config . objectId ] : parsedState ?. state
551+ } ) . catch ( ( err ) =>
552+ this . debugEntity (
553+ `Failed to set setting ${ entity . config . objectId } to value: ${ parsedState ?. state } , reason:` ,
554+ err
555+ )
556+ ) ;
557+ }
564558 break ;
565559 default :
566560 this . debugEntity ( 'Unknown setting:' , entity . config . objectId ) ;
0 commit comments